not all file read.

T

Tony Murphy

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
 
R

Ron Natalie

Tony Murphy said:
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.
 
T

Tony Murphy

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
 
T

Tony Murphy

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
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top