0

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

Rune Aamodt
  • 2,551
  • 2
  • 23
  • 27
user685875
  • 5
  • 1
  • 4

2 Answers2

2

You can use fscanf like this :

int a;

while (fscanf(fptr, "%d", &a) == 1)
{
    printf("Read %d\n", a);
}
user703016
  • 37,307
  • 8
  • 87
  • 112
2
#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 );
}
Avinash
  • 12,851
  • 32
  • 116
  • 186