Fscanf or Fgets? Reading file line after line

I need to write a C program to read a file containing several lines of text, each line contains two variables: number (% f) and line:

EX: file.txt ============ 24.0 Torino 26.0 Milano 27.2 Milano 26.0 Torino 28.0 Torino 29.4 Milano 

There is my code:

 #include <stdio.h> #include <stdlib.h> #include <string.h> int main (int argc, char *argv[]) { int r, line = 0, found = 0; float temp, t_tot = 0; char loc[32]; FILE *fp; fp = fopen(argv[1], "r"); if (fp == NULL) { printf ("Error opening the file\n\n'"); exit(EXIT_FAILURE); } if (argc == 3) { r = fscanf(fp, "%f %s\n", &temp, loc); while (r != EOF) { line++; if (r == 2) { if(strcmp(argv[2], loc) == 0) { t_tot += temp; found++; } } else printf ("Error, line %d in wrong format!\n\n", line); } printf ("The average temperature in %s is: %.1f\n\n", argv[2], (t_tot/found); } } 

The program should read the entire line and find the city that I wrote on argv[2] . Than he will tell me the average temperature in this city, telling me if the line in the file is in the wrong format.

The program compiles correctly for me, but it does not display anything ... how can I solve this? Is fscanf in this case or is fgets better?

I am a student, so please give me an “academic” way to solve this problem :)

+6
source share
4 answers

Few things.

First, you should use fclose ().
Secondly, your code needs fscan () every line in the file. Not only before the while () loop, but in each while loop you need fscan () for the next iteration.
Thirdly, you do not calculate the average temperature, you calculate the sum of all the temperatures found. Fix this by changing "t_tot" to "(t_tot / found)" in your last printf ().

Finally, I'm not sure why you are not getting any result. Is your input like "myprogram file.txt Milano"? It works for me. Anyway, here is your (edited) code back:

 #include <stdio.h> #include <stdlib.h> #include <string.h> int main (int argc, char *argv[]) { int r, line = 0, found = 0; float temp, t_tot = 0; char loc[32]; FILE *fp; fp = fopen(argv[1], "r"); if (fp == NULL) { printf ("Error opening the file\n\n'"); exit(EXIT_FAILURE); } else { if (argc == 3) { r = fscanf(fp, "%f %s\n", &temp, loc); while (r != EOF) { line++; if (r == 2) { if(strcmp(argv[2], loc) == 0) { t_tot += temp; found++; } } else printf ("Error, line %d in wrong format!\n\n", line); r = fscanf(fp, "%f %s\n", &temp, loc); } printf ("The average temperature in %s is: %.1f\n\n", argv[2], (t_tot / found)); } fclose(fp); } } 
+11
source

you should put the fscanf line inside the while loop

  while (1) { r = fscanf(fp, "%f %s\n", &temp, loc); if( r == EOF ) break; ......................... } 

complete the file

if u uses fgets changes as follows

  char s[256]; while( fgets( s, 256, fp) != NULL ) { sscanf( s, "%f %s", &temp, loc); ............. } 
+3
source

Your code does not call fscanf in a loop, as it should: reading is done once, and then the program either exists immediately if the file is empty, or the loop is infinite.

You must move the fscanf call inside the while . One typical way to encode it is to place the destination inside the loop header, for example:

 while ((r = fscanf(fp, "%f %s\n", &temp, loc)) != EOF) { ... } 
+2
source

change code like this ... just put another fscanf statement in while.

 if (argc == 3) { r = fscanf(fp, "%f %s\n", &temp, loc); while(r != EOF ) { r = fscanf(fp, "%f %s\n", &temp, loc); line++; if (r == 2) { if(strcmp(argv[2], loc) == 0) { t_tot += temp; found++; } } else printf ("Error, line %d in wrong format!\n\n", line); } printf ("The average temperature in %s is: %.1f\n\n", argv[2], t_tot); } } 
+1
source

Source: https://habr.com/ru/post/918821/


All Articles