Re: im facing problem with fread()??



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.
.



Relevant Pages

  • porting problem
    ... Socket.h:47: parse error before `)' ... Socket.h:46: conflicts with previous declaration `int Socket::sending' ... Socket.cpp:33: ANSI C++ forbids declaration `memset' with no type ... Socket.cpp:38: ANSI C++ forbids declaration `exit' with no type ...
    (comp.unix.programmer)
  • Re: coloring stdout and stderr
    ... void parent (int stdout_pipe, int stderr_pipe); ... exit; ... argv0, strerror ); ...
    (comp.unix.programmer)
  • Re: how to remove code duplication
    ... A programs that will take input from stdin and put that into log files. ... void create_logname(char *, int); ... exit(EXIT_FAILURE); ...
    (comp.lang.c)
  • Re: how to remove code duplication
    ... the log directory should containing the same files as the out ... exit(EXIT_FAILURE); ... int main ... void process_log_data(FILE *stream) ...
    (comp.lang.c)
  • echo client/server give problems if array sizes are different
    ... client asks for user input and then sends that line to the server ... void echo_back(int); ... exit(EXIT_FAILURE); ...
    (comp.unix.programmer)