   STL list
  Since you can't use the [] operators with the stl lists, how do you use them? I've looked around in the help, but I couldn't figure it out...I'm trying to use a list of a class, and write all the elements of the list to file.  It can't be a vector(which i would like to use) because I need to be able to remove elements from the middle. for(int i = servers.begin(); i = (int)servers.size(); i++){char Serv[MAX_CHARS];sprintf(Serv, "(%s)", servers[i].Name);filed.write((char*)Serv, (streamsize)(strlen(Serv)*sizeof(char)+1));filed.write((char*)servers[i].IP, sizeof(servers[i].IP));filed.write((char*)servers[i].Protocol, sizeof(servers[i].Protocol));filed.close();}It doesnt really surprise me that this doesn't work, but can anyone show me how to make it work...?   oh btw, filed is an fstream; Name, IP, and Protocol are all char arrays._________________________________________"Why, why why does everyone ask ''why'' when ''how'' is so much more fun"-Spawn 1997[edited by - Infuscare on February 13, 2004 11:30:59 PM]
  You use iterators, which are pointer-like objects, returned by member functions like begin() and end(), except that incrementing (or decrementing them, when the container supports it), does "the right thing" (i.e. moves to the next element, regardless of how the container arranges them internally). std::list<Whatever> servers;std::list<Whatever>::iterator itor;for(itor = servers.begin(); itor != servers.end(); ++itor ){  char Serv[MAX_CHARS];  sprintf(Serv, "(%s)", itor->Name);  filed.write((char*)Serv,(streamsize)(strlen(Serv)*sizeof(char)+1));  filed.write((char*)itor->IP,sizeof(itor->IP));  filed.write((char*)itor->Protocol,sizeof(itor->Protocol));  filed.close();}Note that the very same code would work with a vector, a deque ...Also, consider using a stringstream instead of sprintf  Edit - Finally, note that pointers are iterators, for C arrays:int array[100];int* itor;for( itor = array; itor != array+100; ++itor){  // do something to *itor;}  “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor) [edited by - Fruny on February 13, 2004 11:44:28 PM];  Thanks a lot, that really helped...and i use sprintf because its the only way I know how to do it.  How do you do it using a stringstream? I think i read it somewhere sometime, but it didnt really stick... ;  How is that write function gonna work? Unless I''m missing something, if IP and Protocol members are pointers, the sizeof will always return the size of the pointer, not how much data you want to write, but if they are straight members, the (char*) cast is not gonna work. Am I wrong? ;  they are arrays, not pointers... (edit: then again, arrays aren't all that different from pointers, but still, just saying)anyway it works so thats all I really care, you'll have to wait for a somewhat good/more advanced programmer than me to answer you more thoroughly.   [edited by - Infuscare on February 13, 2004 12:08:57 AM];quote:Original post by Infuscare  Thanks a lot, that really helped...and i use sprintf because its the only way I know how to do it.  How do you do it using a stringstream? I think i read it somewhere sometime, but it didnt really stick...    #include <sstream>std::stringstream ss;ss << ''('' << itor->Name << '')'';std::string foo = ss.str(); // get the string// And the cleanup statements, if you want to reuse the streamss.clear();    // clear the *flags* (e.g. EOF) not needed here,// but necessary when you''ve read from the streamss.str("");    // clear the *string*Well, I''ll admit it looks overkill when all you''re doing is add a pair of parentheses, especially here, where just using formatted IO to write the string will work just fine. std::list<Whatever> servers;std::list<Whatever>::iterator itor;for(itor = servers.begin(); itor != servers.end(); ++itor ){  filed << ''('' << itor->Name << '')'';  filed.write((char*)itor->IP,sizeof(itor->IP));  filed.write((char*)itor->Protocol,sizeof(itor->Protocol));  filed.close();}I''ve written a few helper functions that let you write: filed << raw(itor->Protocol); instead of using filed.write - I posted it a while ago in this thread - just scroll down.   “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor)