Re: im facing problem with fread()??
- From: Christopher Benson-Manica <ataru@xxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 24 Aug 2005 14:32:33 +0000 (UTC)
Rajshekhar <rajshekhar3@xxxxxxxxx> wrote:
> #include<stdio.h>
The prototype for exit() is in stdlib.h, which you forgot to include.
> int main()
> {
> FILE *fp1;
> int buf[5];
> int num,i;
> fp1=fopen("triangle.txt","r");
> num=fread(buf,sizeof(int),5,fp1);
Consider what you are doing here. You are asking for the first sizeof
int * 5 bytes of the file (probably 20); if the file contains 6 4 4,
buf[0] will contain four bytes that correspond to the internal
representations of '6', ' ', and '4'. As already noted, you'd be much
better off reading the contents of this file using fgets() and using
strtol() and friends to get your integers.
> printf("num of elements read =%d\n",num);
> for(i=0;i<5;i++)
As a side note, if you know you read num bytes from the file, why not
iterate num times through buf?
> printf("%d\n",buf[i]);
> return 0;
> }
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
.
- Follow-Ups:
- Re: im facing problem with fread()??
- From: l4learn
- Re: im facing problem with fread()??
- References:
- im facing problem with fread()??
- From: Rajshekhar
- im facing problem with fread()??
- Prev by Date: Re: _beginthread -- how to terminate a thread from out side of it.
- Next by Date: Re: how to let gcc warn me when I use = in conditions
- Previous by thread: Re: im facing problem with fread()??
- Next by thread: Re: im facing problem with fread()??
- Index(es):
Relevant Pages
|