470,874 Members | 1,596 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,874 developers. It's quick & easy.

ifstream.get truncaing file

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();

May 23 '06 #1
3 2307
<fa*****@optonline.net> schrieb im Newsbeitrag
news:11**********************@j33g2000cwa.googlegr oups.com...
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

May 23 '06 #2
ahh, thanks

May 23 '06 #3
In article <1148399860.438910.294630
@j33g2000cwa.googlegroups.com>, fa*****@optonline.net
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
May 25 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Dave Johnston | last post: by
6 posts views Thread by Herv? LEBAIL | last post: by
6 posts views Thread by Ram Laxman | last post: by
4 posts views Thread by hall | last post: by
10 posts views Thread by sam | last post: by
1 post views Thread by Xiaozhou.Yin | last post: by
2 posts views Thread by mpalomas | last post: by
11 posts views Thread by adramolek | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.