ifstream.get truncaing file

F

farseer

Hi,
Hi, i am reading the content of a binary file, enoding as base64 and
writing to an output file. After noticing that my output file seems to
be truncated, i created the following simple test code in an attempt to
isolate the issue. The actually binary is 25kb, however the length of
the string appears to be only around 5kb. How can this be possible?

ifstream myfile( argv[1] );

string fc;
while ( myfile.get( ch ) )
{
fc+= ch;
}

cout << "\nContent len: " << fc.length();
 
H

Heinz Ozwirk

Hi,
Hi, i am reading the content of a binary file, enoding as base64 and
writing to an output file. After noticing that my output file seems to
be truncated, i created the following simple test code in an attempt to
isolate the issue. The actually binary is 25kb, however the length of
the string appears to be only around 5kb. How can this be possible?

ifstream myfile( argv[1] );

string fc;
while ( myfile.get( ch ) )
{
fc+= ch;
}

cout << "\nContent len: " << fc.length();

You are reading a binary file in text mode. Use ios::binary to open the
file.

HTH
Heinz
 
J

Jerry Coffin

@j33g2000cwa.googlegroups.com>, (e-mail address removed)
says...

[ ... ]
ifstream myfile( argv[1] );

string fc;
while ( myfile.get( ch ) )
{
fc+= ch;
}

Mostly unrelated to your question, but I'd use something
like this:

// specify we're reading a binary file
std::ifstream myfile(argv[1], std::ios::binary);

// copy entire file into string stream:
std::istringstream temp;
temp << myfile.rdbuf();

// create our string from there:
std::string fc(temp.str());

The speed difference will depend on your compiler and
standard library, but can be _quite_ substantial -- with
Borland 5.5, the speed improvement is often around 10:1,
while with Microsoft it's usually more like 2:1 or 3:1.
You _may_ find that it doesn't give an improvement with
your compiler, but if so it's a pretty unusual case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top