Re: im facing problem with fread()??



Rajshekhar wrote:
hi
 the file triangle.txt contents are...
 6 6 4

thats it .....
i dont think this file is binary file ..!!!


Rajshekhar, please quote what you're replying to in your message. I for one can't always see what it is you're replying to, so sometimes your reply looks like gibberish without context. You'll see this mentioned often in the newsgroup if you've been reading for some time. It makes it much more difficult for people to answer you (and decreases the likelihood that they do)


Looking at the prototype for fread, it is:

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

Based on your program, it tries to read 5*(sizeof int) from the file. That's because it doesn't care about spaces, commas or other punctuation. For that you should look at some other functions such as fscanf, or write your own parser (much more recommended). There have been numerous posts to this newsgroup asking the same question you have in the last month - so please go have a look.

I don't know what the size of an int is on your machine, but it's probably 4 or 8.

From the description I have of 'fread', it says:
"The function fread reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr."


This means, if your file has the contents:

6 6 4

This looks like (in binary)

0x36 0x20 0x36 0x20 0x34 (values are in hex)

So, I expect the first value to be something like 0x36203620 or 0x20362036 depending on the endianness of your machine (one big hex number, that probably looks random).

The fact that you're getting a result of zero from fread() might be because sizeof int is 8 bytes (there aren't 8 bytes in your file triangle.txt).

Indeed, when I compile your program, I get the result:
num of elements read =1
540418102
134482484
-1075253336
134513429
0

Now ignoring the last 4 digits (because num=1), converting 540418102 to hex we have exactly 0x20362036 (I'm running an Intel, little endian - a Mot 68k would give a different result). This is exactly what I had expected.

Also, you have other problems in your code. I'll post it here again:

#include<stdio.h>

/* ** You should include other headers here as well, such as */
#include <stdlib.h>
/* as otherwise exit() is not defined */

/* ** You should use int main(void), in C the empty brackets is discouraged and is the old style. Also the meaning between C and C++ is different so portability is lost */
int main()
{
FILE *fp1;
int buf[5];
int num,i;


    fp1=fopen("triangle.txt","r");
    if(fp1 == NULL)
    {
     printf("file cant be opend");
     exit(0);
    }

    num=fread(buf,sizeof(int),5,fp1);
     printf("num of elements read =%d\n",num);

/* Instead of 'i<5' you should use 'i<num'. That way your not going to access elements of buf[] that aren't initialised. This is why the last 4 values in my output (sizeof int == 4) are also garbage */
for(i=0;i<5;i++)
printf("%d\n",buf[i]);
return 0;
}
.




Relevant Pages

  • Re: im facing problem with fread()??
    ... > Rajshekhar, please quote what you're replying to in your message. ... it tries to read 5*(sizeof int) from the file. ... > been numerous posts to this newsgroup asking the same question you have ... > From the description I have of 'fread', ...
    (comp.lang.c)
  • Re: im facing problem with fread()??
    ... I don't know what the size of an int is on your machine, ... "The function fread reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location ... 0x20362036 depending on the endianness of your machine (one big hex ... hex we have exactly 0x20362036. ...
    (comp.lang.c)
  • Re: Printing the last item of a structure twice
    ... feof is not predictive. ... The first time you call fread, teu is populated with data. ...
    (comp.lang.c.moderated)
  • Re: An interesting thing about fread().
    ... int main ... int rdbytes = 0; ... unsigned char buffer; ... Every time I use fread(), I always assume freadcould be reliable. ...
    (comp.lang.c)
  • Re: compiling error
    ... The first argument to fread() is normally the address of an array, ... no cast is required when converting between void * ... then the compiler automatically converts int* to void* before calling ...
    (comp.lang.c)