【发布时间】:2015-11-02 19:54:47
【问题描述】:
所以我的文件有 full_file_path # 形式的数据,例如,
C:/dev/Java/src/java/util/concurrent/ConcurrentHashMap.java 212
C:/dev/Java/src/java/util/HashMap.java 212
C:/dev/Java/src/java/lang/CharacterData02.java 190
C:/dev/Java/src/java/lang/CharacterData0E.java 190
C:/dev/Java/src/java/nio/DirectCharBufferS.java 123
C:/dev/Java/src/java/nio/DirectCharBufferU.java 123
...
我正在尝试读取文件
int dup;
char file[MAX_LINE];
...
FILE *fp;
fp = fopen("OUTPUT100.txt", "r");
while (fscanf(fp, "%s %d\n", &file, &dup) == 1) {
printf("%s %d\n", file, dup);
}
fclose(fp);
但是输出很垃圾
has 0 duplciate lines of code
RJ9 has 0 duplciate lines of code
has 0 duplciate lines of code
▒▒" has 0 duplciate lines of code
has 0 duplciate lines of code
"A▒ has 0 duplciate lines of code
has 0 duplciate lines of code
7▒cw has 0 duplciate lines of code
has 0 duplciate lines of code
has 0 duplciate lines of code
我做错了什么?
编辑:我的“仍然吐出垃圾”的 cmets 只是脑放屁。我在吐出垃圾的while循环下方的循环中有一个printf。向上滚动几百行后,我开始看到有意义的数据。注释掉 printf 会产生预期的结果。工作读取使用while (fscanf(fp, "%s %d\n", file, &dup) == 2)。谢谢大家。
【问题讨论】:
-
while (fscanf(fp, "%s %d\n", file, &dup) == 1)No&beforefile它已经是一个指针。 -
@DavidC.Rankin,它仍然吐出垃圾。
-
while (fscanf(fp, "%s %d\n", file, &dup) == 2)(很抱歉你的匹配数也被取消了) -
在调用`fopen()时,总是检查(!=NULL)返回值以保证操作成功
-
头文件:
unistd.h公开了众所周知的函数:dup()。发布的代码不符合small, cleanly compiles, has example of the problem的 stackoverflow 标准,因此,除其他外,未知dup()函数是否暴露。