file input using std::string ?

P

Peter Olcott

Does C++ have anything that uses something like a
std::string for text file input?
 
I

Ian Collins

Peter Olcott wrote:

Please don't top-post.
Does C++ have anything that uses something like a
std::string for text file input?

std::string s;
std::cin >> s;
 
O

osmium

"Ian Collins" write:
Peter Olcott wrote:

Please don't top-post.


std::string s;
std::cin >> s;

Based on the OPs question, I suspect he wants to change cin to the name of a
file opened for reading.
 
T

Thomas J. Gritzan

osmium said:
"Ian Collins" write:

Based on the OPs question, I suspect he wants to change cin to the name of a
file opened for reading.

You mean like:

std::string filename = "...";
std::ifstream in(filename.c_str());

std::string s;
in >> s;
 
J

Juha Nieminen

osmium said:
Based on the OPs question, I suspect he wants to change cin to the name of a
file opened for reading.

Another possibility is that he wants to read a string as if it was
an input stream.
If that's the case, then std::stringstream is the answer.
 
J

Jim Langston

Peter Olcott said:
Does C++ have anything like this?

file input using std::string.

It depends on what you mean, like others have said. Do you want to input to
a std::string, or read from a std::string?

Input to a std::string: (untested code)
#include <istream>
#include <string>
int main()
{
std::ifstream Input("Myfile.ext");
std::string Data;
if ( std::getline( Input, Data ) )
// Data now contains one line of text from the file. (deliminated by
'\n')
if ( Input >> Data )
// Data now contains one word form the file (delimited by white space)
}

Read from a std::string:
#include <sstream>
#include <string>

int main()
{
std::string MyString( "This is some text\nJust to show an example" );
std::stringstream MyStream;
MyStream << MyString;
std::string Data;
if ( std::getline( MyStream, Data ) )
// Data should now contain "This is some text"
if ( MyStream >> Data )
// Data should now contain "Just"

}
 
P

Peter Olcott

Jim Langston said:
file input using std::string.

It depends on what you mean, like others have said. Do
you want to input to a std::string, or read from a
std::string?

Input to a std::string: (untested code)
#include <istream>
#include <string>
int main()
{
std::ifstream Input("Myfile.ext");
std::string Data;
if ( std::getline( Input, Data ) )

Yes this is what I was talking about, I need text delimited
by lines.
What is the "if" for ???
 
J

James Kanze


[...]
Yes this is what I was talking about, I need text delimited
by lines.
What is the "if" for ???

To test whether the input succeeded or not. std::getline (like
the << operators) returns a reference to the stream it read;
when used in a condition, the stream will act true if there has
been no error to date, and false if there was some sort of error
(typically, in this case, due to trying to read beyond the end
of file).
 
P

Peter Olcott


[...]
Yes this is what I was talking about, I need text
delimited
by lines.
What is the "if" for ???

To test whether the input succeeded or not. std::getline
(like
the << operators) returns a reference to the stream it read;
when used in a condition, the stream will act true if there
has
been no error to date, and false if there was some sort of
error
(typically, in this case, due to trying to read beyond the
end
of file).

So it would typically be written like this?
while ( std::getline( Input, Data ) )
;


--
James Kanze (GABI Software)
email:[email protected]
Conseils en informatique orientée objet/
Beratung in objektorientierter
Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30
23 00 34
 
J

James Kanze

James Kanze said:
On Jul 26, 3:58 am, "Peter Olcott" <[email protected]>
wrote:
[...]
if ( std::getline( Input, Data ) )
Yes this is what I was talking about, I need text
delimited
by lines.
What is the "if" for ???
To test whether the input succeeded or not. std::getline
(like the << operators) returns a reference to the stream it
read; when used in a condition, the stream will act true if
there has been no error to date, and false if there was some
sort of error (typically, in this case, due to trying to
read beyond the end of file).
So it would typically be written like this?
while ( std::getline( Input, Data ) )
;

Well, the loop typically won't be empty:). But yes, that is
the usual idiom for reading an entire file line by line.
 

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

Forum statistics

Threads
474,290
Messages
2,571,453
Members
48,129
Latest member
DianneCarn

Latest Threads

Top