• Advertisement
Sign in to follow this  

Fstream, using it well.

This topic is 2650 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am having some trouble getting data from a file. I am looking around on google for an answer but not getting too far, so in hopes that this will give me an answer faster

Here is the problem:

open file

read in a line: 0, 20, 293, 0, 1, 999999

store line into


struct Object

bool a;
int b,c,f;
float d,e;

Object( bool A, int B, int C, float D, float E, int F) : a(A), b(B), c(C), d(D), e(E), f(F) {}
};




like

Object myObject = myfile.getline();

Share this post


Link to post
Share on other sites
Advertisement
Off the top of my head:

std::string line;
std::getline( fstream, line, '\n');
int a, b, c, f;
float d, e;
sscanf(line.c_str(),"%d, %d, %d, %f, %f, %d",&a, &b, &c, &d, &e, &f);

Share this post


Link to post
Share on other sites
Doing that all in C++ would involve writing it like:





edit: whoops.... i read that too fast, and didnt see you had different types in there. Fixed my example.
edit edit: *whew* fixed
edit edit edit: *whew* gone!

[Edited by - KulSeran on October 20, 2010 2:08:47 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by KulSeran
Doing that all in C++ would involve writing it like:
*** Source Snippet Removed ***

edit: whoops.... i read that too fast, and didnt see you had different types in there. Fixed my example.


Thanks I was looking at it and at first I was like, ok I get it, close enough but I can get it from there but thank you so much for the updated version because that just makes it that much easier for me to figure out how to do it for my purpose.

Thank you so much :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Buckeye
Off the top of my head:

std::string line;
std::getline( fstream, line, '\n');
int a, b, c, f;
float d, e;
sscanf(line.c_str(),"%d, %d, %d, %f, %f, %d",&a, &b, &c, &d, &e, &f);


No. sscanf in C++ is considered harmful, is less flexible, is slow, and is unreadable. It requires an uneccesary copy from getline. This is not "using fstream well"


Quote:
Original post by KulSeran
Doing that all in C++ would involve writing it like:
*** Source Snippet Removed ***

edit: whoops.... i read that too fast, and didnt see you had different types in there. Fixed my example.
edit edit: *whew* fixed


No. This code is extremely complex, uses templates, and also uses the unnessecary copy from getline. Furthermore, the final api it exposes calls sevral functions and is very unlike C++ built-in text-file serialization api. This is also not "using fstream well"

A good use of fstream for object serialization would utilize a single operator overload on the istream& to serialize the object using standard >> operators from ANY stream type. Furthermore it would avoid un-needed copy constructors and function overhead. Here is my version.


#include <iostream>
#include <fstream>
#include <string>
struct Object
{
bool a;
int b,c,f;
float d,e;

Object( bool A, int B, int C, float D, float E, int F) : a(A), b(B), c(C), d(D), e(E), f(F) {}
Object(){};
};



std::istream& operator>>(std::istream& in,Object& obj)
{
char comma; //storage for useless comma ops;
in >> obj.a >> comma >> obj.b >> comma >> obj.c >> comma >> obj.d >> comma >> obj.e >> comma >> obj.f;
return in; //for chaining inputs
}

std::ostream& operator<<(std::ostream& out,const Object& obj)
{
out << (int)obj.a << ',' << obj.b << ',' << obj.c << ',' << obj.d << ',' << obj.e << ',' << obj.f;
return out; //for chaining outputs;
}

int main(int argc, char **argv)
{
std::ifstream ifile( "test.txt" );

int value = 0;
std::string line;
while(ifile)
{
Object o;
ifile >> o;
//getline(ifile,line); //optional: skip to end of line after parsing object by reading empty chars into line string. This should prevent extra reading of the last line, but it might not
std::cout << o << std::endl;
}

return 0;
}



Share this post


Link to post
Share on other sites
Quote:
Original post by Steve132

No. This code is extremely complex, uses templates, and also uses the unnessecary copy from getline. Furthermore, the final api it exposes calls sevral functions and is very unlike C++ built-in text-file serialization api. This is also not "using fstream well"

A good use of fstream for object serialization would utilize a single operator overload on the istream& to serialize the object using standard >> operators from ANY stream type. Furthermore it would avoid un-needed copy constructors and function overhead. Here is my version.

*** Source Snippet Removed ***


awesome thats exactly what i was trying to do

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement