STL / elegant file input (basic question!)
-
At the moment I'm trying to make an effort to use
std::string
andstd::ifstream
in my work, instead of using"stdio.h"
. My main objective is to write elegant code -- performance is not an issue! :) I need to read a lot of blocks (of various sizes) from file, and append them to variousstring
buffers. The problem is that functions likeifstream::read
are designed to read intochar*
style buffers, notstring
s; however there seems to be some kind of relationship betweenstreambuf
s andstring
s that I figured might make my life easier...? :confused:using namespace std; ... string mystring; ifstream inputfile; int length = 20; ... _// is there a better way to do this...?_ char* buffer = new char [length]; inputfile.read (buffer, length); mystring += buffer; delete[] buffer; _// this isn't much better..._ char ch; for (int i=0; i<length; i++) { inputfile >> ch; mystring += ch; _// presumably I could **reserve** extra space before the loop,_ _// to avoid repeated resizing of the string...?_ }
I have the gut feeling that there must be a really elegant way to do this, using some combination ofstreambuf
/ifstream::operator>>
/ifstream::read
etc. I figured the whole point of usingstd::string
was that it can worry about the allocation side of things behind the scenes. However I'm finding the documentation a little cryptic and generalised. :~ Any ideas? It's quite possible I just want the moon on a stick and I should go back to BASIC... :) -
At the moment I'm trying to make an effort to use
std::string
andstd::ifstream
in my work, instead of using"stdio.h"
. My main objective is to write elegant code -- performance is not an issue! :) I need to read a lot of blocks (of various sizes) from file, and append them to variousstring
buffers. The problem is that functions likeifstream::read
are designed to read intochar*
style buffers, notstring
s; however there seems to be some kind of relationship betweenstreambuf
s andstring
s that I figured might make my life easier...? :confused:using namespace std; ... string mystring; ifstream inputfile; int length = 20; ... _// is there a better way to do this...?_ char* buffer = new char [length]; inputfile.read (buffer, length); mystring += buffer; delete[] buffer; _// this isn't much better..._ char ch; for (int i=0; i<length; i++) { inputfile >> ch; mystring += ch; _// presumably I could **reserve** extra space before the loop,_ _// to avoid repeated resizing of the string...?_ }
I have the gut feeling that there must be a really elegant way to do this, using some combination ofstreambuf
/ifstream::operator>>
/ifstream::read
etc. I figured the whole point of usingstd::string
was that it can worry about the allocation side of things behind the scenes. However I'm finding the documentation a little cryptic and generalised. :~ Any ideas? It's quite possible I just want the moon on a stick and I should go back to BASIC... :)LiquidEyes wrote: Any ideas?
getline(inputfile, mystring);
will read a string from a file. You can pass a third parameter (a character) that specifies a delimiter to use during the reading if you need to. By default, it stops after a newline character.Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
LiquidEyes wrote: Any ideas?
getline(inputfile, mystring);
will read a string from a file. You can pass a third parameter (a character) that specifies a delimiter to use during the reading if you need to. By default, it stops after a newline character.Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
Thanks for the reply. However I don't see how it will know to stop after (say) 20 characters? Without a delimeter, will it read the whole file into the string? I want to do the STL equivalent of
fread( mystring, sizeof(char), length, inputfile );
or failing that, use something more elegant than achar[]
(which I have to allocate myself) as an intermediate buffer. -
Thanks for the reply. However I don't see how it will know to stop after (say) 20 characters? Without a delimeter, will it read the whole file into the string? I want to do the STL equivalent of
fread( mystring, sizeof(char), length, inputfile );
or failing that, use something more elegant than achar[]
(which I have to allocate myself) as an intermediate buffer.From memory I think you can do getline(infile, mystring, "\n") or something. Kevin
-
From memory I think you can do getline(infile, mystring, "\n") or something. Kevin
I don't want it to stop on 'newline'. I want it to read a fixed number of bytes. :confused:
-
Thanks for the reply. However I don't see how it will know to stop after (say) 20 characters? Without a delimeter, will it read the whole file into the string? I want to do the STL equivalent of
fread( mystring, sizeof(char), length, inputfile );
or failing that, use something more elegant than achar[]
(which I have to allocate myself) as an intermediate buffer.LiquidEyes wrote: However I don't see how it will know to stop after (say) 20 characters? It doesn't. You'll have to use the read() method of ifstream to do that. LiquidEyes wrote: Without a delimeter, will it read the whole file into the string? No, as I said, the default delimiter is a newline - it will read a single line.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
LiquidEyes wrote: However I don't see how it will know to stop after (say) 20 characters? It doesn't. You'll have to use the read() method of ifstream to do that. LiquidEyes wrote: Without a delimeter, will it read the whole file into the string? No, as I said, the default delimiter is a newline - it will read a single line.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
You'll have to use the read() method of ifstream to do that. Precisely. Which takes us back to my original question ... what structure can I
read
into instead of an old fashionedchar*
array? I just thought there might be an elegant way, usingvector
s orstring
s or something, that didn't involve allocatingchar
s manually. As I said, maybe I just want the moon on a stick. :) -
You'll have to use the read() method of ifstream to do that. Precisely. Which takes us back to my original question ... what structure can I
read
into instead of an old fashionedchar*
array? I just thought there might be an elegant way, usingvector
s orstring
s or something, that didn't involve allocatingchar
s manually. As I said, maybe I just want the moon on a stick. :)LiquidEyes wrote: what structure can I read into instead of an old fashioned char* array? Just quickly looking at the docs, it appears you can use
ifstream::getline(mystring, number_of_chars)
. If you pass a size of, say, 10, it reads 9 characters and appends a NULL. Once again, you can add a third parameter which is the delimiter if you need it.Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
I don't want it to stop on 'newline'. I want it to read a fixed number of bytes. :confused:
I couldn't find that getline with no. of bytes that Ray mentioned. I suppose you could use the getline that gets the whole line and then use a substring function. Not as elegant but still avoids char*. Kevin