fread returns more than i want

F

Franz Jeitler

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
 
U

Ulrich Eckhardt

Franz said:
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
 
?

=?ISO-8859-1?Q?Bj=F8rn_Augestad?=

Franz said:
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
 
E

Eric Sosman

Franz said:
[...]
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.
 
F

Franz Jeitler

Ulrich Eckhardt said:
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
 
F

Franz Jeitler

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


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

Franz
 
U

Ulrich Eckhardt

Franz said:
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
 
E

Eric Sosman

Franz said:
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);
 
F

Franz Jeitler

Eric Sosman said:
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);

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

Thank you

Franz Jeitler
 
E

Eric Sosman

Franz said:
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);


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().
 

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

Similar Threads

fread/fwrite 2 18
error 28
fread() 6
A Question of Style 51
strdup + fread = fail? 5
fread() 2
Can't solve problems! please Help 0
URGENT 1

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top