fread() #2

  • Follow


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[i]!=EOF ; ++i)
        printf("%c" , buffer[i]);
    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!
0
Reply hossein.rohani (6) 1/23/2008 7:09:36 PM

h03Ein wrote:

> 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[i]!=EOF ; ++i)
>         printf("%c" , buffer[i]);
>     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.

0
Reply santosh.k83 (3969) 1/23/2008 7:23:06 PM


"h03Ein" <hossein.rohani@gmail.com> wrote in message
> 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[i]!=EOF ; ++i)
>        printf("%c" , buffer[i]);
>    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.

-- 
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

0
Reply regniztar (3128) 1/23/2008 7:23:47 PM

Malcolm McLean wrote:

> 
> "h03Ein" <hossein.rohani@gmail.com> wrote in message
>> 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[i]!=EOF ; ++i)
>>        printf("%c" , buffer[i]);
>>    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>

0
Reply santosh.k83 (3969) 1/23/2008 7:36:22 PM

h03Ein wrote:

> 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[i]!=EOF ; ++i)
Make it: (see below)
      for (i = 0; i < c; i++)
>         printf("%c" , buffer[i]);
Why? putchar(buffer[i]) 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.

-- 
Army1987 (Replace "NOSPAM" with "email")
0
Reply army1987 (668) 1/23/2008 8:30:07 PM

In article <fn846t$amn$1@registered.motzarella.org>,
santosh  <santosh.k83@gmail.com> wrote:
>h03Ein wrote:
>
>> 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

-- 
Dave Vandervies                                dj3vande at eskimo dot com
Why does all this bother you with parachutes and not with cars? Many cars
can take you past terminal velocity.
                           --Maarten Wiltink in the scary devil monastery
0
Reply dj3vande3 (264) 1/23/2008 8:30:54 PM

h03Ein wrote:
> 
> 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[i]!=EOF ; ++i)
>         printf("%c" , buffer[i]);
>     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'.

-- 
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: <http://cbfalconer.home.att.net>
            Try the download section.



-- 
Posted via a free Usenet account from http://www.teranews.com

0
Reply cbfalconer (19183) 1/24/2008 2:11:50 AM

6 Replies
43 Views

(page loaded in 0.274 seconds)

Similiar Articles:













7/28/2012 1:55:21 AM


Reply: