473,230 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 software developers and data experts.

file input using std::string ?

Does C++ have anything like this?
Jul 21 '07 #1
11 2404
Peter Olcott wrote:
Does C++ have anything like this?
Like what?

--
Ian Collins.
Jul 21 '07 #2
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
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
"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
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
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
"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

"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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
4
by: Dave | last post by:
Hello all, The scheme shown below to move a text file's contents into a std::string works with one exception: it drops the carriage return and line feed characters. How may I, in a...
2
by: Neil Zanella | last post by:
Hello, Consider the following program. There are two C style string stack variables and one C style string heap variable. The compiler may or may not optimize the space taken up by the two stack...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
6
by: Khuong Dinh Pham | last post by:
How do i read the contents of a xml file and output it to a std::string. Thx in advance
14
by: sriram | last post by:
hi everybody, i need a way other than using library funcs to in put a string as password in c++ directly thro the cin object. if any body finds it pl tel me.
6
by: SpreadTooThin | last post by:
How can I read in two characters from a file into a std::string? the long way: fstream f("myfile.bin", ios::binary | ios:in); // Opened for read binary... unsigned char buffer; buffer =...
11
by: Angus | last post by:
I am working with a C API which often requires a char* or char buffer. If a C function returns a char* I can't use string? Or can I? I realise I can pass a char* using c_str() but what about...
5
by: rohdej | last post by:
Hello - I have been all over the web and found a few posts that are somewhat related to what I'm trying to do, but none that provided me a concise answer. I want to prompt the user to input the...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.