473,240 Members | 1,748 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,240 software developers and data experts.

not all file read.

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
3 2753

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

Similar topics

5
by: simon place | last post by:
is the code below meant to produce rubbish?, i had expected an exception. f=file('readme.txt','w') f.write(' ') f.read() ( PythonWin 2.3 (#46, Jul 29 2003, 18:54:32) on win32. ) I got...
6
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive...
3
by: Abhas | last post by:
> > Hi, this is Abhas, > > I had made a video library program in C++, but was facing a problem. > > After entering 12 movies, i cannot enter any more movies. > > Something gibberish comes instead....
8
by: siliconwafer | last post by:
Hi All, If I open a binary file in text mode and use text functions to read it then will I be reading numbers as characters or actual values? What if I open a text file and read it using binary...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
13
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it...
9
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
15
by: patf | last post by:
Hi - experienced programmer but this is my first Python program. This URL will retrieve an excel spreadsheet containing (that day's) msci stock index returns. ...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...

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.