473,320 Members | 2,107 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,320 software developers and data experts.

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 2419
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave Johnston | last post by:
Hi, I'm currently trying to create a wrapper that uses C functions but behaves like ifstream (from fstream.h) - this is because the platform I'm using (WinCE) doesn't support streams and this is...
6
by: Herv? LEBAIL | last post by:
Hi everybody, I'm writing a program which use the <string>, <vector> and <ifstream> classes. Given an array of string, i.e vector<string> file_names, example : file_names = "file1.txt"...
6
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber"...
6
by: csvka | last post by:
Hello, I wonder if I could pick your brains. I'm beginning to learn about C++. I have opened a file in my program and I want to read lines from it. I would like this to be done in a separate...
4
by: hall | last post by:
Hi. I ran across a bug in one of my problems and after spending some time tracking it down i found that the problem arose in a piece of code that essentially did this: ----------- ifstream...
10
by: sam | last post by:
Hi, Can anyone tell me how to print a file name from ifstream? the following cout code does not print the filename I created with ifstream preivous: ifstream is; is.open ("text.txt");
1
by: Xiaozhou.Yin | last post by:
Hi~ In the program,I first used the ifstream variable fin open the file and,open it again after called the fin.close().But the fin is fieled to open the file in the second time.The book I'm...
2
by: mpalomas | last post by:
Hi C++ folks, I have trouble to open files whose path contains non-ascii characters with std::ifstream. For instance let's say i just have a file which has Japanese characters either in the...
11
by: adramolek | last post by:
So... I'm trying to get used to using C++ ifstream (or ofstream) instead of stdio (although I'm having second thoughts). Anyways, I want to be able to display a meaningful error message if ifstream...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.