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

not all file read.

P: n/a
Strange problem, I read a file into a string using ifstream,
ostringstream and string and end part of the file is missing (file
size ~9.5kb, ~9k read). its a html file. using windows nt 4

------------- Not working right
----------------------------------------
ifstream file(filename.c_str());
if(!file) {
logError("FAILURE: Opening file %s",filename.c_str());
}
ostringstream buffer;
buffer << file.rdbuf();
string dataFromFile(buffer.str());

if(file.bad()) {
logError("Failed to read file %s, state=%d",filename,file.rdstate());
}

printf("%s",dataFromFile.c_str());

------------------------------------------------------------------------

then i print out the contents of file.rdbuf and the whole file is
printed to screen.

file.clear();
file.seekg(0, ios::beg);
cout << file.rdbuf() << endl;

-------------------------------------------------------------------------

this code only reads a portion of the file as well

file.clear();
file.seekg(0, ios::beg);
char l_buffer[10240];
memset(l_buffer,0,sizeof(l_buffer));
file.read( l_buffer, 10240 );
prinf("%s",l_buffer);

-------------------------------------------------------------------------
anyone know whats going on. if the file is smaller (~3kb), then i've
no problems. I tried c stdlib function fread(,,,) as well, and had
similar problem.

anyone point me to c++ faq and a good website. i have the c++
programming language by bjarne stroustrup, which is good, but need a
book with good examples & more of a tutorial rather than a reference.

thanks
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Tony Murphy" <to*********@yahoo.com> wrote in message news:aa**************************@posting.google.c om...
printf("%s",dataFromFile.c_str());


Does the file by any chance include a null byte? That will cause the printf
to stop there.

How about:
cout.write(dataFromFile.data(), dataFromFile.size());
as a test.

DId you look at the size of the string? It may n ot be the reading of the file
that is broken but your printing it out again.
Jul 19 '05 #2

P: n/a
you are right ron, there is a null byte in the file. now must go and
find out how it got there, how to find it and remove it. i reckon the
difference in file size is related to carriage return/linefeed

"Ron Natalie" <ro*@sensor.com> wrote in message news:<3f***********************@news.newshosting.c om>...
"Tony Murphy" <to*********@yahoo.com> wrote in message news:aa**************************@posting.google.c om...
printf("%s",dataFromFile.c_str());


Does the file by any chance include a null byte? That will cause the printf
to stop there.

How about:
cout.write(dataFromFile.data(), dataFromFile.size());
as a test.

DId you look at the size of the string? It may n ot be the reading of the file
that is broken but your printing it out again.

Jul 19 '05 #3

P: n/a
problem seems to be caused by mixing const char * and string and
assignment, haven't go to the bottom of it yet.

is
string x(const char*) a valid constructor?

file is been read correctly into the string, the problem surfaces when
i use string.c_str(), string.data() is ok. i now don't think there is
a null terminator anywhere in the file
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.