Hi everybody,
I have some 50 bytes lines in a text file containing something like this:
Code:
0 111444550 sam fisher -1
I want to pass it to a function to parse it. Looked like my best bet was stringstream. Here is my code:
Code:
fstream hashstream;
stringstream buffer;
hashstream.open("primaryfile.txt");
hashstream.read (buffer,50);
somestruct = parseLine(buffer)
mystruct parseLine(stringstream rawline)
{
int DEL;
string SSN;
long int PTR;
rawline.seekg(0, ios::beg);
rawline>>DEL;
rawline.ignore();
rawline>>SSN;
for (i=0;i<31;i++)
rawline.ignore();
rawline>>PTR;
return mystruct; //contains the 3 variables. edited function for clarity
}
Note: It goes int, 3 strings, long int. the first/last name are padded to 10 and 20 chars respectively (with blank spaces) and all vars are separated with one blank space.
What if I want to write to the file using a function?
Reading from it is easy since I just copy to a buffer and pass that. Writing using a function would require opening/closing a file stream which might need to be different than the one in int main().
What's the best way to handle it?
Any way to do it easily with pointers? without having to pass stuff back and forth?
Do I still use fstream without arguments to read and write?
Thanks for your time.