Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old May 23rd, 2006, 05:05 PM
farseer@optonline.net
Guest
 
Posts: n/a
Default 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();

  #2  
Old May 23rd, 2006, 05:25 PM
Heinz Ozwirk
Guest
 
Posts: n/a
Default Re: ifstream.get truncaing file

<farseer@optonline.net> schrieb im Newsbeitrag
news:1148399860.438910.294630@j33g2000cwa.googlegr oups.com...[color=blue]
> 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();[/color]

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

HTH
Heinz

  #3  
Old May 23rd, 2006, 06:05 PM
farseer@optonline.net
Guest
 
Posts: n/a
Default Re: ifstream.get truncaing file

ahh, thanks

  #4  
Old May 25th, 2006, 11:55 PM
Jerry Coffin
Guest
 
Posts: n/a
Default Re: ifstream.get truncaing file

In article <1148399860.438910.294630
@j33g2000cwa.googlegroups.com>, farseer@optonline.net
says...

[ ... ]
[color=blue]
> ifstream myfile( argv[1] );
>
> string fc;
> while ( myfile.get( ch ) )
> {
> fc+= ch;
> }[/color]

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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.