473,395 Members | 1,401 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,395 software developers and data experts.

file input using std::string ?

Does C++ have anything like this?
Jul 21 '07 #1
11 2409
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.