reading an integer from a file
I have a code which suppose to read an int开发者_如何学运维eger 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
#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 );
}
You can use fscanf
like this :
int a;
while (fscanf(fptr, "%d", &a) == 1)
{
printf("Read %d\n", a);
}
精彩评论