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

Reading a file into a string

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.