473,402 Members | 2,050 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,402 software developers and data experts.

Read contents of text file.

How do you read the contents of a text file into a std::string? Does this
code use the right classes and functions for the job?

std::string s;
char c;
while (is.get(c))
s += c;

Thanks.
Jul 23 '05 #1
5 2870
Well, if you need the entire contents of the file in a single string,
your loop will work, though there are other ways of doing it. You could
use getline for example:

std::string file;
std::string line;

while (std::getline(is, line))
file += line;

Or you could use a stringstream and rdbuf:

std::ostringstream oss;
oss << in.rdbuf();
std::string file = oss.str();

There's no 'right' way to do it, just try whatever comes to mind and
see if it does what you want and has acceptable performance.

Jul 23 '05 #2
"James Daughtry" <mo*******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Well, if you need the entire contents of the file in a single string,
your loop will work, though there are other ways of doing it. You could
use getline for example:

std::string file;
std::string line;

while (std::getline(is, line))
file += line;

Or you could use a stringstream and rdbuf:

std::ostringstream oss;
oss << in.rdbuf();
std::string file = oss.str();

There's no 'right' way to do it, just try whatever comes to mind and
see if it does what you want and has acceptable performance.


Ok. I'll use my loop for the time being.
Jul 23 '05 #3
>while (std::getline(is, line))
file += line;


I believe you should add the newline caracter at the end of each line:

while (std::getline(is, line))
file += line + "\n";

Good luck,

Marcelo Pinto

Jul 23 '05 #4

"Jason Heyes" <ja********@optusnet.com.au> wrote in message news:42**********************@news.optusnet.com.au ...
How do you read the contents of a text file into a std::string? Does this
code use the right classes and functions for the job?

[snip]

Reading file to string (29 ways):
http://groups-beta.google.com/group/...5e065030?hl=en

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
Jul 23 '05 #5
It depends on how you plan to use it. For situations where the newline
would be needed, it's usually better to place each line in a container
of strings rather than one big string. I left the decision open since
the OP didn't specify how the string was going to be used.

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David Thomas | last post by:
Hi there, a while ago, I posted a question regarding reading japanese text from a text file. Well, since I solved the problem, I thought I'd post my solution for the benefit of other people with...
5
by: ashton | last post by:
Hello, I have a file (mytest.fle) it is just a text file with a different extension. I would like to be able to open this file through my C++ application and read it in as a string. For...
4
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void...
3
by: JenHu | last post by:
Hi, I want read line by line and characters. The characters are fix length text file, no specific delimited method between each fields. The first line is header line, the last line is footer. ...
4
by: Ernesto | last post by:
I'm just want to read in the contents of a (text) file. The text file is filled with semiColon delimited floating point strings... 0.456;1.265;99.742;... For some reason, I can't get the...
7
by: Hallvard B Furuseth | last post by:
I'm trying to clean up a program which does arithmetic on text file positions, and also reads text files in binary mode. I can't easily get rid of it all, so I'm wondering which of the following...
7
by: theballz | last post by:
Hi, I am learning c programming and come across a problem i cant seem to solve. I have a file which i wish to parse and put certain lines (which do not contain a hash character) into an array...
3
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
I try to follow Steve's paper to build a database, and store a small text file into SQL Server database and retrieve it later. Only difference between my table and Steve's table is that I use NTEXT...
6
ak1dnar
by: ak1dnar | last post by:
Hi, I have created Sub in VB.net application, Which reads a text file contents. Sub IPsetter() Dim fileName As String Dim realIParray As String() = Nothing Dim...
4
by: Keith G Hicks | last post by:
I'm trying to read a text file and alter the contents of specific lines in the file. I know how to use streamreader to read each line of a file. I'm doing that already to get the data into a...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.