[英]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.我有 c 代码在 while 循环中读取文件,fscanf 将所有信息放入 url,num 和 rank 变量,然后打印出来。 However, the output is incorrect.
但是,output 不正确。
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:预计 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.如果您已经在使用
fscanf
(只需继续扫描直到找到EOF
)以及在 while 循环中,则不需要使用fgets
。 Also, you need to properly define the ,
as a delimiter otherwise the first %s
will read commas into the string as well.此外,您需要正确定义
,
作为分隔符,否则第一个%s
也会将逗号读入字符串。 This while
loop works:这个
while
循环有效:
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
).我将分隔符定义为逗号,因此第一个字符串应该读取字符,直到通过
%10[^,]
遇到逗号(如格式说明符中所述,最多读取 10 个字符),后跟一个逗号,integer,逗号, float 然后是换行符 ( \n
)。
Output: 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).我做了一些更改,因为您的代码中的行给了我错误(可能来自编译器或 IDE,不确定)。 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. % 5 [^,]
表示一直读到最多遇到五个字符或逗号。
For example, because url31
has 5 characters, I gave this so you can give different numbers for different operations.例如,因为
url31
有5个字符,所以我给了这个,所以你可以为不同的操作给不同的数字。
#include <stdio.h> #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);
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.