Reputation: 73
I am trying to print out the first 3 line from a text file.
states 10
start 0
accept 9
Code:
int main(int argc, char*argv[]){
FILE *fp;
fp = fopen(argv[1], "r");
printf("File is open successfully.\n");
char *ptr, buf[256];
int states; //states
int start; //start
int accept; //accept
while((ptr = fgets(buf, 256, fp)) != NULL){
printf("%s", ptr);
//process this line from the file
}
printf("============================\n");
char s1[100], s2[100], s3[100];
fscanf("%s %i",s1, &states);
fscanf("%s %i",s2, &start);
fscanf("%s %i",s3, &accept);
printf("states: %i\n start: %i\n accpet: %i\n", states, start, accept);
}
The output I am receiving:
states: 32528
start: -29575952
accpet: 32767
After printing out the what's in the file. and prints out the random number, I cannot seem to get the states, start, and accept number.
Thanks.
Upvotes: 0
Views: 666
Reputation: 378
It seems you don't use fscanf properly. The first argument should be file descriptor:
int fscanf(FILE *stream, const char *format, ...)
you can see usage (for example) at: http://www.tutorialspoint.com/c_standard_library/c_function_fscanf.htm
Upvotes: 1