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

fread returns more than i want

Hello,

I have some problems with fread.. first, let's see a part of the source
file:

FILE *fp;
char buf[512];
size_t nread;
..
..
while( nread = fread(buf,1,sizeof(buf),fp) )
fwrite(buf,1,nread,stdout);

The code above is working fine, it returns a desired file on the screen.

Now the problem:
------------------
For a file server I need to give back a structure like: <
strcpy(result.text, buf) >;

For easier understanding, temporary I can also write:
< fprintf(stdout, buf) > instead of < fwrite(buf,1,nread,stdout) >.

But now at the end of each part of the file I get some strange characters
and the last block continues again
with the first lines of the file.

So, why does this happen and how can I solve the problem?

Thank's

Franz
Dec 4 '05 #1
9 2348
Franz Jeitler wrote:
For easier understanding, temporary I can also write:
< fprintf(stdout, buf) > instead of < fwrite(buf,1,nread,stdout)


No. fprintf() takes a zero-terminated string, while fwrite takes a buffer
of raw bytes. Further, fprintf() also looks at the string and interprets
it, which might not be the right thing.

Uli

Dec 4 '05 #2
Franz Jeitler wrote:
Hello,

I have some problems with fread.. first, let's see a part of the source
file:

FILE *fp;
char buf[512];
size_t nread;
..
..
while( nread = fread(buf,1,sizeof(buf),fp) )
fwrite(buf,1,nread,stdout);

The code above is working fine, it returns a desired file on the screen.

Now the problem:
------------------
For a file server I need to give back a structure like: <
strcpy(result.text, buf) >;

For easier understanding, temporary I can also write:
< fprintf(stdout, buf) > instead of < fwrite(buf,1,nread,stdout) >.

But now at the end of each part of the file I get some strange characters
and the last block continues again
with the first lines of the file.

So, why does this happen and how can I solve the problem?


I'm guessing here, as you didn't provide a complete program. fread()
does not terminate the buffer with a string terminator, so when you
strcpy() of fprintf(), you copy/write more characters than expected.

BTW, fprintf(stdout, buf) is a really bad idea,
fprintf(stdout, "%s", buf) is much better.
HTH
Bjørn
Dec 4 '05 #3
Franz Jeitler wrote:
[...]
For a file server I need to give back a structure like: <
strcpy(result.text, buf) >;

For easier understanding, temporary I can also write:
< fprintf(stdout, buf) > instead of < fwrite(buf,1,nread,stdout) >.

But now at the end of each part of the file I get some strange characters
and the last block continues again
with the first lines of the file.

So, why does this happen and how can I solve the problem?


You get a different interpretation if you regard buf as
a zero-terminated string than if you regard it as an array
of nread characters. In particular, if buf lacks a zero
terminator it is not a proper string, and passing it to
fprintf() invokes undefined behavior. (A likely result is
that fprintf() will "run off the end" of buf until it stumbles
upon a zero byte somewhere else in memory; you are probably
seeing the characters that precede that zero.)

Also, if '%' appears anywhere in buf, fprintf() will try
to interpret it as a formatting directive ...

Your best bet is to use fwrite(), as you apparently intend
to do eventually in any case.

--
Eric Sosman
es*****@acm-dot-org.invalid
Dec 4 '05 #4

"Ulrich Eckhardt" <do******@knuut.de> schrieb im Newsbeitrag
news:3v*************@uni-berlin.de...
Franz Jeitler wrote:
For easier understanding, temporary I can also write:
< fprintf(stdout, buf) > instead of < fwrite(buf,1,nread,stdout)


No. fprintf() takes a zero-terminated string, while fwrite takes a buffer
of raw bytes. Further, fprintf() also looks at the string and interprets
it, which might not be the right thing.

Uli


OK, I understand, so should I terminate the string manually (even if I
return a binary char-Array
in < result.text> ?

Franz
Dec 4 '05 #5

"Eric Sosman" <es*****@acm-dot-org.invalid> schrieb im Newsbeitrag
news:2t********************@comcast.com...
Your best bet is to use fwrite(), as you apparently intend
to do eventually in any case.

--
Eric Sosman
es*****@acm-dot-org.invalid

Sorry, but I have to write the block into a structure for returning it to a
client, so what do you suggest?

Franz
Dec 4 '05 #6
Franz Jeitler wrote:
"Ulrich Eckhardt" <do******@knuut.de> schrieb im Newsbeitrag
news:3v*************@uni-berlin.de...
Franz Jeitler wrote:
For easier understanding, temporary I can also write:
< fprintf(stdout, buf) > instead of < fwrite(buf,1,nread,stdout)


No. fprintf() takes a zero-terminated string, while fwrite takes a
buffer of raw bytes. Further, fprintf() also looks at the string
and interprets it, which might not be the right thing.


OK, I understand, so should I terminate the string manually (even if I
return a binary char-Array
in < result.text> ?


No, use the right functions for whatever you are doing. If you have a
buffer containing text, that text will usually be followed by a null
character, if there isn't one, you might add it. If your buffer contains
other data, it might be perfectly possible that there is a null character
in the middle - functions only reading a buffer up to the first null char
are then not the suitable tool to handle such data.

Uli
Dec 4 '05 #7
Franz Jeitler wrote:
"Eric Sosman" <es*****@acm-dot-org.invalid> schrieb im Newsbeitrag
news:2t********************@comcast.com...
Your best bet is to use fwrite(), as you apparently intend
to do eventually in any case.

--
Eric Sosman
es*****@acm-dot-org.invalid


Sorry, but I have to write the block into a structure for returning it to a
client, so what do you suggest?


What does the client expect to receive from you? If
the struct contains the bytes, like this:

struct {
size_t count;
char bytes[512];
} client_data;

.... then you can just read the data directly into the
struct, like this:

client_data.count = fread(client_data.buf, 1,
sizeof client_data.buf, fp);

--
Eric Sosman
es*****@acm-dot-org.invalid
Dec 4 '05 #8

"Eric Sosman" <es*****@acm-dot-org.invalid> schrieb im Newsbeitrag
news:kJ********************@comcast.com...
char bytes[512];
} client_data;

... then you can just read the data directly into the
struct, like this:

client_data.count = fread(client_data.buf, 1,
sizeof client_data.buf, fp);

--
Eric Sosman
es*****@acm-dot-org.invalid


Oh yes, I think that might work, because my client code has < printf ("%s",
result.text) instead of < fread...>.

Thank you

Franz Jeitler
Dec 4 '05 #9
Franz Jeitler wrote:
"Eric Sosman" <es*****@acm-dot-org.invalid> schrieb im Newsbeitrag
news:kJ********************@comcast.com...
char bytes[512];
} client_data;

... then you can just read the data directly into the
struct, like this:

client_data.count = fread(client_data.buf, 1,
sizeof client_data.buf, fp);

--
Eric Sosman
es*****@acm-dot-org.invalid

Oh yes, I think that might work, because my client code has < printf ("%s",
result.text) instead of < fread...>.


Aha! The client expects you to supply a zero-terminated
string, not a counted array of bytes. You should be using a
string-oriented input function like fgets(), not the array-
oriented fread().

--
Eric Sosman
es*****@acm-dot-org.invalid
Dec 4 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Meenakshi Matai | last post by:
I am trying to read data from a .dat file using fread. This is how my syntax looks: temp = fread(indata, sizeof(short int), 1, datafile); I looked into the data file in Hex format and saw that...
2
by: Josh Wilson | last post by:
The fread() is what I believe is having trouble, but I do not know why. I know that the temp file is created and written to (and w/ the appropriate amount of data). fread() continues to return 0,...
4
by: bill | last post by:
I am confused by the behavior of the following code. The function copy() takes two FILE *'s and copies the data from one to the other. In the main routine, I create a pipe and copy stdin to the...
13
by: 010 010 | last post by:
I found this very odd and maybe someone can explain it to me. I was using fread to scan through a binary file and pull bytes out. In the middle of a while loop, for no reason that i could...
20
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file...
30
by: empriser | last post by:
How to use fread/fwrite copy a file. When reach file's end, fread return 0, I don't konw how many bytes in buf.
2
by: steve005 | last post by:
Hi, I am writing a binary file with matlab that consists of a couple hundred double values and then reading it in with c++. The problem arises in c++ when I try and read in the files. At a specific...
20
by: xiao | last post by:
Hi~ every one~ I have a queston about fread function. if i have a code like this: (nscrdh and data are defined as two dementional arrays and both of them were stored in the same binary file) ...
15
by: =?ISO-8859-15?Q?L=E9na=EFc?= Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all, For some reasons, somewhere in a program, I'd like, if possible, to quickly parse a whole file before rewinding it and letting the...
1
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.