Re: im facing problem with fread()??




Rajshekhar wrote:
> Hi ,
> i am writing a simple prgm to read a .txt file then store the contents
> into the array...
Please post the contents of triangle.txt.then someone can help you.
If triangle.txt is a binary file then try rb instead of r in fopen.

> program as follows:
> --------------------------
> #include<stdio.h>
>
> 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);
>
> for(i=0;i<5;i++)
> printf("%d\n",buf[i]);
> return 0;
> }
>
> ------------------
> i am getting no.of elements read as 0,but file is openable...
> the elements as junk numbers...
>
> can anybody tell me wat is the problem in doing this ..????
> is it that i m using fread wrongly or it behaves abnoramlly???
>
> TIA
> Regards,
> Rajshekhar

.



Relevant Pages

  • Re: read keyboard input and storing in an array?
    ... > I'm trying to store user input in an array, ... You have the beginnings of that logic already since your prompt tells the ... 201st value since the array only has room for 200 values. ... This will force you to store the int values as Integer ('Integer' ...
    (comp.lang.java.help)
  • Re: attempting an actual game...
    ... >>> and inflexible by the absurd decision to use a bit array for square ... as then one has 8 bits in which to store a color and a few flags ... Using a 2D int (or, ... > Change direction and you may eventually complete a game. ...
    (comp.games.development.programming.misc)
  • Re: Adding large numbers in C
    ... one of the numbers - or perhaps the result - is too big to store in an int. ... you have bitstrings longer than 8 bits, simply use an array of unsigned ... Incidentally, the subtraction routine does similar juggling, so if M and N ...
    (comp.lang.c)
  • Re: Reading a Text file
    ... int main ... In any case, you said earlier you want to store the data in an array, ... don't store its result in a char. ...
    (comp.lang.c)
  • Re: array allocaton size
    ... > int x; ... > then I create an array of fred: ... The compiler needs to store somewhere how many elements are ... This information is needed at destruction time ...
    (comp.lang.cpp)