简体   繁体   中英

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:

  • Why floats are just zeros and how can to fix this?
  • How to remove comma after url? Url can be any length (char url[10] is an example). Would it be better to split the line first on every comma and then use fscanf to add information into variable?

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;
}    

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

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);

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM