Search code examples
carraysfileintegerstrtol

reading an integer from a file


I have a code which suppose to read an integer from a file. But its actually reading as an character. Suggest me some modification where I can read the integers into an array.

fptr =fopen("path","r");

while(1)
{
  c=getc(fptr);
  putchar(c);
  if (c==EOF)
    exit(1);
}

Thanks in advance

Amit


Solution

  • #include <stdio.h>
    int main(int argc, char **argv ) {
        int value;
        FILE *fp = fopen ( "d:\\abc.txt", "r");
        while ( fscanf(fp, "%d", &value) == 1 ) {       
            printf ( "%d\n", value );
        }
        fclose ( fp );
    }