fread()

H

h03Ein

Hi!
how many characters will be printed in output if our input file has
just 3 characters?
Sinppet Code :
#include<stdio.h>
main()
{
char buffer[1024];
char input[] = "some file"
FILE *fp = fopen(input , "r");
int c = fread(buffer , 1 , 1024, fp);
int i = 0;
for(i = 0 ; buffer!=EOF ; ++i)
printf("%c" , buffer);
fclose(fp);
}
the result for me is my 3 input characters in file plus lots of
strange characters why?
does fread consider EOF while reading from file?
Thanks!
 
S

santosh

h03Ein said:
Hi!
how many characters will be printed in output if our input file has
just 3 characters?
Sinppet Code :
#include<stdio.h>
main()
{
char buffer[1024];
char input[] = "some file"
FILE *fp = fopen(input , "r");

I hope you actually check that the above call succeeded?
int c = fread(buffer , 1 , 1024, fp);
int i = 0;

Mixed declarations and code are new to C99, which isn't implemented
widely yet.
for(i = 0 ; buffer!=EOF ; ++i)
printf("%c" , buffer);
fclose(fp);
}
the result for me is my 3 input characters in file plus lots of
strange characters why?
does fread consider EOF while reading from file?


No. It returns the count of characters that it actually read, which may
be less than what you asked it for or zero. To find out if it was
end-of-file or an error that was responsible for the short count you
must use feof() or ferror() after the fread() call.

Specifically in the above for loop you must consider only the first 'i'
characters of buffer as valid.
 
M

Malcolm McLean

h03Ein said:
Hi!
how many characters will be printed in output if our input file has
just 3 characters?
Sinppet Code :
#include<stdio.h>
main()
{
char buffer[1024];
char input[] = "some file"
FILE *fp = fopen(input , "r");
int c = fread(buffer , 1 , 1024, fp);
int i = 0;
for(i = 0 ; buffer!=EOF ; ++i)
printf("%c" , buffer);
fclose(fp);
}
the result for me is my 3 input characters in file plus lots of
strange characters why?
does fread consider EOF while reading from file?
Thanks!

fread() will stop reading when it encounters EOF.
The return value is the number of items read successfully, and anything
afterwards is garbage - in your case, strange characters.
The EOF is not itself placed into the buffer and is in any case an integer.
If you think about it, it is necessary for EOF to be wider than a character,
if every character is to be representable.
 
S

santosh

Malcolm said:
h03Ein said:
Hi!
how many characters will be printed in output if our input file has
just 3 characters?
Sinppet Code :
#include<stdio.h>
main()
{
char buffer[1024];
char input[] = "some file"
FILE *fp = fopen(input , "r");
int c = fread(buffer , 1 , 1024, fp);
int i = 0;
for(i = 0 ; buffer!=EOF ; ++i)
printf("%c" , buffer);
fclose(fp);
}
the result for me is my 3 input characters in file plus lots of
strange characters why?
does fread consider EOF while reading from file?
Thanks!

fread() will stop reading when it encounters EOF.


<pedantic>
When it encounters end-of-file condition. EOF is the object like macro
used to signal end-of-file /or/ error to user code, and it is /not/
used by fread().
</pedantic>

<snip>
 
A

Army1987

h03Ein said:
Hi!
how many characters will be printed in output if our input file has
just 3 characters?
Sinppet Code :
#include<stdio.h>
main()
{
char buffer[1024];
char input[] = "some file"
FILE *fp = fopen(input , "r");
You should verify that it succeeds:
if (fp == NULL) {
perror("Cannot open file");
exit(EXIT_FAILURE);
}
or whatever is appropriate. exit and EXIT_FAILURE are declared/defined in
stdlib.h.
int c = fread(buffer , 1 , 1024, fp);
fread returns a size_t. You're using 1024 bytes, and int is required to
hold numbers to 32767, so, in this case, it is OK, but, in general, I
would use a size_t. Also, decimal constants ("magic numbers") tend to make
programs harder to understand. Use sizeof buffer in place of 1024 on that
line. Or use #define BUFFERSIZE 1024, declare buffer as `char
buffer[BUFFERSIZE]`, and use fread(buffer, 1, BUFFERSIZE, fp);
int i = 0;
for(i = 0 ; buffer!=EOF ; ++i)

Make it: (see below)
for (i = 0; i < c; i++)
printf("%c" , buffer);

Why? putchar(buffer) would do that. Do you use a hammer to plant
drawing pins?
BTW, you could just use fwrite(buffer, 1, c, stdout); to do the same thing.
fclose(fp);
}
the result for me is my 3 input characters in file plus lots of strange
characters why?
EOF is a negative integer. If char is signed, that loop will stop when
hitting a valid character, e.g. ÿ. If it is unsigned, it will go on until
the end of the memory you're allowed to read, and then the behavior is
undefined. See www.c-faq.com, section 12.
does fread consider EOF while reading from file?
It returns the numbers of characters successfully read, so the loop should
be changed as above.
 
D

dj3vande

h03Ein said:
Hi!
how many characters will be printed in output if our input file has
just 3 characters?
Sinppet Code :
#include<stdio.h>
main()
{
char buffer[1024];
char input[] = "some file"
FILE *fp = fopen(input , "r");

I hope you actually check that the above call succeeded?
int c = fread(buffer , 1 , 1024, fp);
int i = 0;

Mixed declarations and code are new to C99, which isn't implemented
widely yet.

I don't see any mixed declarations and code here. A bunch of
initializations that would be better done after the declarations, yes,
but all the executable code above this point is in the
initializations.

If he was mixing declarations and code, he'd have a program that was
not valid under any C standard, since he's implicit-inting main (which
went away in C99).


dave
 
C

CBFalconer

h03Ein said:
how many characters will be printed in output if our input file
has just 3 characters?

Sinppet Code :
#include<stdio.h>
main() {
char buffer[1024];
char input[] = "some file"
FILE *fp = fopen(input , "r");
int c = fread(buffer , 1 , 1024, fp);
int i = 0;
for(i = 0 ; buffer!=EOF ; ++i)
printf("%c" , buffer);
fclose(fp);
}
the result for me is my 3 input characters in file plus lots of
strange characters why? does fread consider EOF while reading
from file?


Yes, but it is a characteristic of the streamed device, not of the
char stream. The value in c should be 3 if only 3 chars were
read. What you have in buffer is not a string, because it lacks a
final '\0'.
 

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


Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top