reading a file into std::string

B

Bo Persson

Nobody said:
<istream>, <ostream> and <iostream> are headers, not classes,
although classes with those names exist.

Also, std::endl() is a function, not a method.

And even better (or worse?) is that iostream inherits from both
istream and ostream, not the other way round.


Bo Persson
 
J

Joshua Maurice

It's not guaranteed in C++03, but I believe all current implementations
do so.

Well, it's (perhaps?) not guaranteed that strings always store their
contents in contiguous memory, but you can ask for the contents o the
string in a contiguous piece of memory with the member function c_str.
 
J

Joshua Maurice

Stroustrup, section 20.3.15, page 598

    The getline() function reads a line terminated by eol into its
string, expanding the string as needed to hold the line. If no eol
argument is provided, a newline '\n' is used as the delimiter. The line
terminator is removed from the stream but not entered into string.

where string = 2nd argument, eol = 3rd argument

now my point is when you give '\0' (its called null or NULL ?) as 3rd
argument which is not present in the input stream, what will be its
behavior ?  

(1) will getline() keep on looking for it till its reaches EOF (End of
File) and read whole file into the string  

(2) If I give anything which is not present in the input e.g. '#' will it
behave the same ?

Yes to question 1 (if there are no null characters in the file), and
yes to question 2. std::getline is agnostic towards its third
argument. It keeps reading characters from the input stream until it
finds the third argument, drops that character, and puts the read
characters (except for the delimiter) into the output argument
string.

Also, IIRC if it reaches EOF before encountering its third argument,
then if it has read at least one character before EOF into the output
string, then it marks the EOF bit on the input stream, but does not
mark the fail bit nor the bad bit. If it read 0 characters into the
output string and it encounters EOF, then it marks the EOF bit and the
fail bit (but not the bad bit) on the input stream.
 
N

Nobody

And even better (or worse?) is that iostream inherits from both
istream and ostream, not the other way round.

Right. istream and ostream inherit from ios, the common base class for
all streams. iostream is the input-AND-output stream class (i.e.
read-write streams, analogous to fopen()'s "plus" modes: r+/w+/a+).
 
J

Juha Nieminen

Joshua Maurice said:
Well, it's (perhaps?) not guaranteed that strings always store their
contents in contiguous memory, but you can ask for the contents o the
string in a contiguous piece of memory with the member function c_str.

It doesn't help in this case because it returns a const char*.
 
J

Jean-Luc Romano

I want to read a file into std::string. I am basically a C Programmer so
it was quite hard for me to understand how to do it in C++. I did C++
long time back (if you guys remember my name but I do remember Shiva and
Victor Bazarov and others).

I googled for it and this is the best what I could come up with. Do you
guys have any suggestion for improvement ?


Maybe this has already been mentioned, but I've done it this way
several times:


std::ifstream inFile("reference.cpp" /*, std::ios::binary*/);
if (!inFile.is_open())
{
std::cerr << "Error reading file." << std::endl;
return;
}
const std::string fileContents
= std::string(std::istreambuf_iterator<char>( inFile ),
std::istreambuf_iterator<char>( ));
inFile.close();


Now the fileContents variable contains the contents of
"reference.cpp", while still allowing for the possibility that the
file might not successfully open.

Cheers,

-- Jean-Luc
 
L

Lynn McGuire

Maybe this has already been mentioned, but I've done it this way
several times:


std::ifstream inFile("reference.cpp" /*, std::ios::binary*/);
if (!inFile.is_open())
{
std::cerr<< "Error reading file."<< std::endl;
return;
}
const std::string fileContents
= std::string(std::istreambuf_iterator<char>( inFile ),
std::istreambuf_iterator<char>( ));
inFile.close();


Now the fileContents variable contains the contents of
"reference.cpp", while still allowing for the possibility that the
file might not successfully open.

Cheers,

-- Jean-Luc

Does that do a copy into fileContents ? If so, will your
memory be double for a while ?

Lynn
 
N

Nana J. Osei

Gee, thank you very much. This one liner magic works! I owe you one. Do you
accept bitcoins? Just a token of appreciation...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top