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

Reading a file into a string

My goal is to efficiently read a file into a c++ string, discarding
newlines. Here is my first cut:

bool MyClass::ReadFile( string strFileName )
{
ifstream File( strFileName.c_str() );
if ( !File )
{
return false;
}
else
{
m_strFileData.reserve( 10000 );
char Char;
while ( File.get( Char ) )
{
if ( Char != '\n' )
m_strFileData += Char;
}
File.clear();
File.close();
return true;
}
}

I'm sure there's a better (i.e., faster) way. Can anyone help out?
Thanks!
Jul 19 '05 #1
2 53998

"Nobody You Know" <ap*******@hushmail.com> wrote in message
news:24**************************@posting.google.c om...
My goal is to efficiently read a file into a c++ string, discarding
newlines. Here is my first cut:

bool MyClass::ReadFile( string strFileName )
{
ifstream File( strFileName.c_str() );
if ( !File )
{
return false;
}
else
{
m_strFileData.reserve( 10000 );
char Char;
while ( File.get( Char ) )
{
if ( Char != '\n' )
m_strFileData += Char;
}
File.clear();
File.close();
return true;
}
}

I'm sure there's a better (i.e., faster) way. Can anyone help out?
Thanks!


Instead of reading character by character I'd propose to use istream
iterators.

ifstream InFile( "detlog.txt" );
if( !InFile ) {
cerr << "Couldn´t open input file" << endl;
return false;
}

// create reader objects
istream_iterator<string> DataBegin( InFile );
istream_iterator<string> DataEnd;

Now you can iterate using these istream_iterators and append their values to
a string for example:

while( DataBegin != DataEnd ) {
m_strFileData += *DataBegin;
DataBegin++;
}

HTH
Chris
Jul 19 '05 #2
ap*******@hushmail.com (Nobody You Know) writes:
My goal is to efficiently read a file into a c++ string, discarding
newlines. Here is my first cut:

bool MyClass::ReadFile( string strFileName )
{
ifstream File( strFileName.c_str() );
if ( !File )
{
return false;
}
else
{
m_strFileData.reserve( 10000 );
char Char;
while ( File.get( Char ) )
{
if ( Char != '\n' )
m_strFileData += Char;
}
File.clear();
File.close();
return true;
}
}

I'm sure there's a better (i.e., faster) way. Can anyone help out?
Thanks!


Use getline:

#include <fstream>
#include <iostream>
#include <string>

// read contents of file "bla" into buf, discarding newlines
int main(int argc, char* argv[]) {
std::string buf;
std::string line;
std::ifstream in("bla");
while(std::getline(in,line))
buf += line;
std::cout << "read: " << buf << "\n";
return 0;
}

HTH & kind regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
Jul 19 '05 #3

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

Similar topics

19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
6
by: Dave Reid | last post by:
Hi everyone... I'm pretty much a newbie C++ user, and I've run into a problem. I'm trying to read in a large text file, and then do manipulations on it. I can read it into a large 2-dimensional...
2
by: Claire | last post by:
This works ok in a new empty project. I write an empty string to a file. Looking at the file with a hex editor, there's a single byte of value zero. I expect this, it's utf8 encoding and this is...
111
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- ...
2
by: jimmy | last post by:
Hi, I have an XML string that has been returned using a WebRequest object that i now need extract some data from. Some sample data is shown below. <?xml version="1.0" encoding="UTF-8"?>...
7
by: random guy | last post by:
Hi, I'm writing a program which creates an index of text files. For each file it processes, the program records the start and end positions (as returned by tellg()) of sections of interest,...
1
by: Mannie | last post by:
Hi! In my C++ application I use a COM Object (written in C#). When I try to call one of the methods (that tries to read settings from configuration file) I get a nasty error -0x7c812a5b...
3
by: miss time | last post by:
Hi all, my java friends ^-^ I have next week quiz in reading file text ,and understand the topic very well. can any one give some question related to this topic .this help me more to...
1
Coldfire
by: Coldfire | last post by:
Hi, The strange problem i am having is, the input element of type='file' not reading file names after 20 file elements. It simple returns null on reading the 'name' of file. The code is...
1
by: bjoarn | last post by:
I have an Application C# handling file reading, building index on this file, using dll wrapped with SWIG. The dll is originaly programmed in C++. Dll reports back to the the C# programm throug...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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
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...

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.