【发布时间】:2017-04-25 23:35:38
【问题描述】:
为了正确解析输入,我需要能够计算文件中的行数。但是,我不想计算只是换行符的行。为了解决这个问题,我创建了以下函数:
int countLinesInFile(char *filename) {
int newlines = 0;
if (access(filename,F_OK) != -1)
error("File not found",0);
FILE *input = fopen(filename,"r");
int size = 256 * 4;
char buffer[size];
while ((fgets(buffer,sizeof(buffer),input)) != EOF) {
printf("Read a string");
if (buffer == "\n")
continue;
newlines++;
}
fclose(input);
return newlines;
}
在文件的顶部,我有以下内容:
#include <stdio.h>
#include <unistd.h>
当我运行程序并尝试计算行数时,它会出现分段错误。使用 valgrind,我可以看到以下内容:
==6632== Invalid read of size 4
==6632== at 0x4EA8E6B: fgets (in /usr/lib64/libc-2.24.so)
==6632== by 0x402219: countLinesInFile (in [executable])
[other information about program, does not seem relevant]
==6632== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==6632==
==6632==
==6632== Process terminating with default action of signal 11 (SIGSEGV)
==6632== Access not within mapped region at address 0x0
==6632== at 0x4EA8E6B: fgets (in /usr/lib64/libc-2.24.so)
==6632== by 0x402219: countLinesInFile (in [executable])
[other information about program, does not seem relevant]
==6632== If you believe this happened as a result of a stack
==6632== overflow in your program's main thread (unlikely but
==6632== possible), you can try to increase the size of the
==6632== main thread stack using the --main-stacksize= flag.
==6632== The main thread stack size used in this run was 8388608.
==6632==
==6632== HEAP SUMMARY:
==6632== in use at exit: 475 bytes in 16 blocks
==6632== total heap usage: 19 allocs, 3 frees, 3,075 bytes allocated
==6632==
==6632== LEAK SUMMARY:
==6632== definitely lost: 0 bytes in 0 blocks
==6632== indirectly lost: 0 bytes in 0 blocks
==6632== possibly lost: 0 bytes in 0 blocks
==6632== still reachable: 475 bytes in 16 blocks
==6632== suppressed: 0 bytes in 0 blocks
==6632== Rerun with --leak-check=full to see details of leaked memory
==6632==
==6632== For counts of detected and suppressed errors, rerun with: -v
==6632== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
我试图在 while 循环的开头添加一行“printf(“Reading file”)”。这段代码不执行,我相信 fgets 是问题所在。不幸的是,我不知道这个问题是什么。
我已验证我要读取的文件确实包含正确的文本,并且不为空。
我创建的函数是这个任务的正确方法吗?如果是这样,我会遇到什么问题?以后如何避免这个问题?
更新:这对我来说确实是一个愚蠢的错误。我正在使用 Valgrind 运行程序,看起来它不使用可执行文件的目录,这意味着它找不到文件。感谢您的帮助。
【问题讨论】:
-
首先检查
fopen的返回值。 -
buffer == "\n"这不是C中比较字符串的方式。需要使用strcmp或字符比较buffer[0] == '\n' -
fgets不返回EOF。 -
@BLUEPIXY 我检查了 fopen 的输出,它说“找不到文件”,尽管有一个“stuff.br”(正在打开的文件的名称,我检查了)同一个目录。
-
该文件是否与可执行文件存在于同一目录中?在 IDE 中运行时,往往不是同一个目录。尝试指定文件名的完整路径。另外,如果您输入用户的文件名,您是否正在删除换行符?