Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > not all file read.

Reply
Thread Tools

not all file read.

 
 
Tony Murphy
Guest
Posts: n/a
 
      11-06-2003
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
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      11-06-2003

"Tony Murphy" <> wrote in message news: 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.


 
Reply With Quote
 
 
 
 
Tony Murphy
Guest
Posts: n/a
 
      11-07-2003
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" <> wrote in message news:<3faa828f$0$75672$ om>...
> "Tony Murphy" <> wrote in message news: 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.

 
Reply With Quote
 
Tony Murphy
Guest
Posts: n/a
 
      11-07-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
![Oof Topix] All McCain, All Flip-Flop, All the Time Bucky Breeder Computer Support 7 09-27-2008 03:33 PM
'All Programs' index: can not read it all. (repost - problems withmy ng client) CocoLuke Computer Support 3 05-05-2008 03:53 PM
XP 'All Programs' does not show all programs & I can be adjust it CocoLuke Computer Support 3 05-04-2008 08:20 AM
Can not delete file: Can not read from source file or disk. Pundit Computer Support 0 02-28-2005 03:27 AM
Need to concatenate all files in a dir together into one file and read the first 225 characters from each file into another file. Tony Perl Misc 5 04-19-2004 03:28 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57