J Szum
J Szum

Reputation: 515

Reading file with fscanf in C

I have c code that reads the file in a while loop, fscanf all the information into url, num and rank variables, and then prints it out. However, the output is incorrect.

Main questions:

I have a file containing following information:

url31, 3, 0.2623546
url21, 1, 0.1843112
url34, 6, 0.1576851
url22, 4, 0.1520093
url32, 6, 0.0925755
url23, 4, 0.0776758
url11, 3, 0.0733884 

This is what I get:

Link: url21,; Number: 1; Rank: 0.000000
Link: url34,; Number: 6; Rank: 0.000000
Link: url22,; Number: 4; Rank: 0.000000
Link: url32,; Number: 6; Rank: 0.000000
Link: url23,; Number: 4; Rank: 0.000000
Link: url11,; Number: 3; Rank: 0.000000
Link: url11,; Number: 3; Rank: 0.000000

Expected output:

Link: url31; Number: 3; Rank: 0.2623546
Link: url21; Number: 1; Rank: 0.1843112
Link: url34; Number: 6; Rank: 0.1576851
Link: url22; Number: 4; Rank: 0.1520093
Link: url32; Number: 6; Rank: 0.0925755
Link: url23; Number: 4; Rank: 0.0776758
Link: url11; Number: 3; Rank: 0.0733884

The code I have:

#define MAXSTR 1000

int main () {

    FILE *file;

    char url[10];
    int num;
    float rank;

    if ((file = fopen("pages.txt", "r")) == NULL) {
        printf("Error.\n");
        return -1;
    }

    while(fgets(lines, MAXSTR, file) != NULL) {

        fscanf(file, "%s %d %f", &url[0], &num, &rank);
        printf("Link: %s; Number: %d; Rank: %f\n", url, num, rank); 

    }
    return 0;
}    

Upvotes: 0

Views: 645

Answers (2)

I made some changes because the lines in your code gave me errors (probably from compiler or IDE, not sure). It works this way because the comma is not considered a space character.

% 5 [^,] means read until you encounter a maximum of five characters or commas.

  • For example, because url31 has 5 characters, I gave this so you can give different numbers for different operations.

    #include <stdio.h>

    int main () {

      FILE *fp;
    
      char url[10];
    
      int num;
    
      float rank;
    
      fp = fopen("pages.txt" , "r");
      if(fp == NULL) {
          perror("Error opening file");
          return(-1);
      }
          while(fscanf(fp, "%5[^,], %d, %f\n", url, &num, &rank) != EOF) {
          printf("Link: %s; Number: %d; Rank: %f\n", url, num, rank);
      }
      fclose(fp);
    
      return(0);
    

    }

Upvotes: 0

Jarvis
Jarvis

Reputation: 8564

You don't need to use fgets if you are already using fscanf (just keep scanning until you find an EOF) as well inside your while loop. Also, you need to properly define the , as a delimiter otherwise the first %s will read commas into the string as well. This while loop works:

while(fscanf(file, "%10[^,], %d, %f\n", url, &num, &rank) != EOF) {
    printf("Link: %s; Number: %d; Rank: %f\n", url, num, rank); 
}

I defined the delimiter as a comma, so the first string should read the characters until a comma is encountered via %10[^,] (read upto 10 characters as mentioned in the format-specifier), followed by a comma, integer, comma, float and then a newline (\n).

Output:

Link: url31; Number: 3; Rank: 0.262355
Link: url21; Number: 1; Rank: 0.184311
Link: url34; Number: 6; Rank: 0.157685
Link: url22; Number: 4; Rank: 0.152009
Link: url32; Number: 6; Rank: 0.092575
Link: url23; Number: 4; Rank: 0.077676
Link: url11; Number: 3; Rank: 0.073388

Upvotes: 4

Related Questions