pouët.net

help - reusable file loader

category: code [glöplog]
 
I'm trying to write a reusable function for loading text/html files, and saving them as one big string. Not sure why this crashes. I've been looking at this piece for waay too long now, and I'm gonna bash my head in at any moment. Any help would be most appreciated.

Code: //take in a filename, return a string of the files contents string load_file(string filename){ ifstream infile(filename.c_str()); string file_string; string lines[999]; int i = 0; while(! infile.eof()){ getline(infile,lines[i]); file_string += lines[i]; i++; } infile.close(); infile.clear(); return file_string; }
added on the 2010-06-23 01:20:50 by xteraco xteraco
Why are you using a table instead of a single temp string? Doesn't look like you'll be using them anyways.

Also double check your filenames and -locations.
added on the 2010-06-23 01:28:06 by msqrt msqrt
Oh, and that getline() takes a reference as the second parameter, amirite?
added on the 2010-06-23 01:30:22 by msqrt msqrt
By table I'm guessing you mean array? Its the only thing I really understand how to work with. Getline that is. Earlier I was using infile.get() to get one char at a time, but that was even worse.
added on the 2010-06-23 01:32:11 by xteraco xteraco
Code: const std::string load_file(const std::string& filename) { std::string str((std::istreambuf_iterator<char>( std::ifstream(filename) )), std::istreambuf_iterator<char>()); return str; }
hey by the way, now that we're on the topic of "even ridiculously simple things are difficult in c++", can anyone tell me why rasmus uses "const std::string& filename" and not "std::string const& filename" in that function?
added on the 2010-06-23 08:07:20 by skrebbel skrebbel
i wasn't aware that you could place the const keyword anywhere else than in front of the definition.

but parsing by reference into a function that is only looking at the value (i.e. filename) should be fixed with both const and &.

returning might as well be const since the compiler should invoke the copy-constructor anyhows when you do a std::string mycontent=load_file("foo.txt");
Quote:
hey by the way, now that we're on the topic of "even ridiculously simple things are difficult in c++"


they are not if you understand what you're doing and how a computer works. it all comes down to properly formatting your code.

Code: using namespace std; ... const string load_file(const string& filename) { return string(istreambuf_iterator<char>(ifstream(filename)), istreambuf_iterator<char>()); }


not very hard, isn't it?

and concerning your question: because C++ syntax says so?
added on the 2010-06-23 09:16:07 by hcdlt hcdlt
Quote:
return string(istreambuf_iterator<char>(ifstream(filename)), istreambuf_iterator<char>());

not very hard, isn't it?


You ARE joking, aren't you?!
added on the 2010-06-23 09:46:34 by KeyJ KeyJ
Quote:

Quote:

Code:return string(istreambuf_iterator<char>(ifstream(filename)), istreambuf_iterator<char>());


not very hard, isn't it?

You ARE joking, aren't you?!


why should i be joking? this code is only considered "hard" when you didn't care to have a look at the documentation of the STL string class.
personally, i think that - whatever xteraco is trying to do - he should not use c++, except when he is trying to learn it at the moment. his piece of code implies that he would better be using java, python, php or any other language where you don't have to consider memory management.
and by the way, i don't find reading a file into a string a "ridiculously simple thing". because even when it might be at a first glance, there are things like file encoding, string/character formats and memory usage to be taken into consideration. a "ridiculously simple thing" for me would be assigning a value to a variable or copying memory, which in c/c++ are: *tadaaaaaa* ridiculously simple!
added on the 2010-06-23 10:08:01 by hcdlt hcdlt
what is the hard part? the messy part is just syntax. i ask the compiler to generate code that will take an input file stream, read it as chars and store the result in a string.
Quote:
what is the hard part? the messy part is just syntax. i ask the compiler to generate code that will take an input file stream, read it as chars and store the result in a string.


i have a feeling that KeyJ wants it to look something like "string str = file(filename);". you might want to use a scripting language for that if you don't like c++'s more machine-oriented syntax.
added on the 2010-06-23 10:31:49 by hcdlt hcdlt
xteraco:
You should use string streams for string building, not "+=". (#include <sstream>)
Also the close() and clear() are not needed because the file is automatically closed once infile goes out of scope.

This should make your code work:

Code://take in a filename, return a string of the files contents string load_file(string filename){ ifstream infile(filename.c_str()); ostringstream outStr; string lineString; while(! infile.eof()){ getline(infile,lineString); outStr << lineString; } return outStr.str(); }
added on the 2010-06-23 11:33:06 by Sinar Sinar
i would figure out why it crashes first, like with a debug if possible
my first guess would be an i >= 999 ...
added on the 2010-06-23 12:06:19 by hcdlt hcdlt
:)
Thanks everyone for the help! Also Sinar, thanks for the explanation. I was using close and clear just because I couldn't figure out why it was crashing hehe. Sort of out of desperation.

I'm just learning all of this as I go. After I finish this project, I'm gonna hit up a book on C++. I find if I just get done with a project before studying it gives me more context.

Thanks again. =]
added on the 2010-06-23 14:19:26 by xteraco xteraco

login