   [C++] File Processing (ifstream)
  If I'm writing values to a file, separated by something... let's say, a dash or a comma.. How can I then read the file back in, without those common characters and just read in the values?  For example, I'll have a line in the file like this:1, 2, 3, 4, Blabla, 1, 2, 20/04/08How do I read in that line and write the values to the struct without any of the commas interrupting?Thanks   
  You read the commas and ignore them.   ;  Could you elaborate please? How do you "ignore" them?edit: ie, through string manipulation or is there something else I can use?   ;  If all your records have the format you specified, then something like this:struct Record{   int a, b, c, d;   std::string text;   int e, f;   std::string date;};std::istream &operator>>(std::istream &in, Record &record){    char comma;    in >> record.a >> comma >> record.b >> comma  >> record.c >> comma >> record.d >> comma;    // read a string up until a comma is hit.    std::getline(in,record.text,',');    in >> record.e >> comma >> record.f >> comma;    std::getline(in,record.date);    return in;}This of course doesn't do much error checking (any non-numeric character will work, not just a comma, doesn't check the date format). Actually, the above would require the fields be "packed": 1,2,3,4,Blabla,1,2,20/04/08.It could be written to handle the version you gave, with spaces, but that is the general idea.   ;  I would suggest writing the structure into the file and then just reading out the structure in it's binary from.The best part about C/C++ is the ability to use pointers.  Say you've written 3 structures to a file.  You load the entire file into memory and then can just set a pointer to the start locations of each of the structures.char* cLoadedBuffer;STRUCT1* pStruct1;STRUCT2* pStruct2;STRUCT3* pStruct3;pStruct1 = reinterpret_cast<STRUCT1*>(cLoadedBuffer);pStruct2 = reinterpret_cast<STRUCT2*>(cLoadedBuffer[SIZEOF(STRUCT1)]);pStruct3 = reinterpret_cast<STRUCT3*>(cLoadedBuffer[SIZEOF(STRUCT1) + SIZEOF(STRUCT2)]);Now this will not work if it's a file you want to ship pre-built with your application on multiple platforms but it will work if it's a file you save after installation on any platform.  If you're just going to target a single platform, loading like this will work.Happy coding.NOTE:  I'm not sure this will work if you're storing string classes in the structure.  I'm a C#/ActionScript programmer now and haven't done any type of file manipulation in C/C++ in quite some time.  If you just have a particular sized buffer for the string in the structure, it will work.[Edited by - LordShade on April 19, 2008 10:49:18 AM];  Basically I have about 4 classes, 3 derived from the base.  I've managed to get the saving down sorted by using dynamic casting but loading these up again and putting them back into my linked list is proving difficult.  The only thing I can think of is to save a string in the file that is associated to the class.  So I'll search that line for a string, and load based upon that string.   ;Quote:Original post by LordShadeI would suggest writing the structure into the file and then just reading out the structure in it's binary from.The best part about C/C++ is the ability to use pointers.  Say you've written 3 structures to a file.  You load the entire file into memory and then can just set a pointer to the start locations of each of the structures.char* cLoadedBuffer;STRUCT1* pStruct1;STRUCT2* pStruct2;STRUCT3* pStruct3;pStruct1 = reinterpret_cast<STRUCT1*>(cLoadedBuffer);pStruct2 = reinterpret_cast<STRUCT2*>(cLoadedBuffer[SIZEOF(STRUCT1)]);pStruct3 = reinterpret_cast<STRUCT3*>(cLoadedBuffer[SIZEOF(STRUCT1) + SIZEOF(STRUCT2)]);Now this will not work if it's a file you want to ship pre-built with your application on multiple platforms but it will work if it's a file you save after installation on any platform.  If you're just going to target a single platform, loading like this will work.Happy coding.NOTE:  I'm not sure this will work if you're storing string classes in the structure.  I'm a C#/ActionScript programmer now and haven't done any type of file manipulation in C/C++ in quite some time.  If you just have a particular sized buffer for the string in the structure, it will work.This is an unnecessarily complicated answer especially for the beginners forum.  Rip off's code is much closer (I'm assuming) to what the OP is looking for.   ;Quote:Original post by gregrampageThis is an unnecessarily complicated answer especially for the beginners forum.  Rip off's code is much closer (I'm assuming) to what the OP is looking for.I'll have to disagree here.  If you're writing in C/C++ you need to have a fundamental understanding of pointers.Besides, who wants to spend time writing overly verbose saving and loading routines when you can dedicate that time working on physics or graphical routines?  :)Happy coding.;Quote:The best part about C/C++ is the ability to use pointers.Ha.Anywho, the problem with your serialization method Lordshade is that it will only work correctly for trivial types, which renders it pretty much useless as far as C++ goes. (It looks like you're used to using C idioms, which is fine, except that C and C++ are very different languages).Unfortunately, serialization in C++ requires a bit more care (as always, the C++ FAQ Lite has a lot of good information on this topic).   ;  Very true.  But he was talking about a structure.  Serialization does get complicated when attempting to save classes rather than simple types.  I would like to point out that files aren't saved for a particular class-based architecture, they are saved as data chunks with designated size and meaning.  So you can load large portions of your file and use pointers to iterate through the known data format for construction of your class-based execution model.My intent was to show a way to load data without having to read 1, 2, or 4 bytes at a time from your file.   