By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,265 Members | 1,710 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,265 IT Pros & Developers. It's quick & easy.

file input using std::string ?

P: n/a
Does C++ have anything like this?
Jul 21 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Peter Olcott wrote:
Does C++ have anything like this?
Like what?

--
Ian Collins.
Jul 21 '07 #2

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

"Ian Collins" <ia******@hotmail.comwrote in message
news:5g*************@mid.individual.net...
Peter Olcott wrote:
>Does C++ have anything like this?
Like what?

--
Ian Collins.

Jul 21 '07 #3

P: n/a
Peter Olcott wrote:

Please don't top-post.
"Ian Collins" <ia******@hotmail.comwrote in message
news:5g*************@mid.individual.net...
>Peter Olcott wrote:
>>Does C++ have anything like this?
Like what?
Does C++ have anything that uses something like a
std::string for text file input?
std::string s;
std::cin >s;

--
Ian Collins.
Jul 21 '07 #4

P: n/a
"Ian Collins" write:
Peter Olcott wrote:

Please don't top-post.
>"Ian Collins" <ia******@hotmail.comwrote in message
news:5g*************@mid.individual.net...
>>Peter Olcott wrote:
Does C++ have anything like this?

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

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.
Jul 21 '07 #5

P: n/a
osmium wrote:
"Ian Collins" write:
>Peter Olcott wrote:
>>Does C++ have anything that uses something like a
std::string for text file input?
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.
You mean like:

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

std::string s;
in >s;

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Jul 22 '07 #6

P: n/a
osmium wrote:
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.
Jul 22 '07 #7

P: n/a
"Peter Olcott" <No****@SeeScreen.comwrote in message
news:96*******************@newsfe13.phx...
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"

}
Jul 22 '07 #8

P: n/a

"Jim Langston" <ta*******@rocketmail.comwrote in message
news:kc************@newsfe03.lga...
"Peter Olcott" <No****@SeeScreen.comwrote in message
news:96*******************@newsfe13.phx...
>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 ) )
Yes this is what I was talking about, I need text delimited
by lines.
What is the "if" for ???
// 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"

}

Jul 26 '07 #9

P: n/a
On Jul 26, 3:58 am, "Peter Olcott" <NoS...@SeeScreen.comwrote:
"Jim Langston" <tazmas...@rocketmail.comwrote in message
news:kc************@newsfe03.lga...
[...]
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).

--
James Kanze (GABI Software) email:ja*********@gmail.com
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

Jul 26 '07 #10

P: n/a

"James Kanze" <ja*********@gmail.comwrote in message
news:11**********************@b79g2000hse.googlegr oups.com...
On Jul 26, 3:58 am, "Peter Olcott" <NoS...@SeeScreen.com>
wrote:
"Jim Langston" <tazmas...@rocketmail.comwrote in message
news:kc************@newsfe03.lga...
[...]
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 ) )
;
--
James Kanze (GABI Software)
email:ja*********@gmail.com
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
Jul 27 '07 #11

P: n/a
On Jul 27, 3:57 am, "Peter Olcott" <NoS...@SeeScreen.comwrote:
"James Kanze" <james.ka...@gmail.comwrote in message
news:11**********************@b79g2000hse.googlegr oups.com...
On Jul 26, 3:58 am, "Peter Olcott" <NoS...@SeeScreen.com>
wrote:
"Jim Langston" <tazmas...@rocketmail.comwrote in message
>news:kc************@newsfe03.lga...
[...]
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
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

Jul 27 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.