C语言 在 unix 上读取文件时如何解决退出代码 139 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42882168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve exit code 139 error when reading from file on unix
提问by michael lee
So I believe this is just a problem on unix and that it occurs at the first fscanf if the Clion debugger was right, but I don't know why I get the error- Process finished with exit code 139 (interrupted by signal 11: SIGSEGV) - why?
所以我相信这只是 unix 上的一个问题,如果 Clion 调试器是正确的,它会发生在第一个 fscanf 上,但我不知道为什么我会收到错误 - 进程已完成,退出代码为 139(被信号 11 中断:SIGSEGV ) - 为什么?
struct loginInformation
{
char username[USERNAME_LENGTH];
char password[PASSWORD_LENGTH];
int type;
}accounts[NUM_OF_ACCOUNTS];
void createAccountsFromFile()
{
FILE *input = fopen("accounts.txt", "r");
int counter;
for(counter = 0; counter < NUM_OF_ACCOUNTS; counter++)
{
fscanf(input, "%s", accounts[counter].username);
fscanf(input, "%s", accounts[counter].password);
fscanf(input, "%d", &accounts[counter].type);
}
}
int main()
{
createAccountsFromFile();
}
accounts.txt
user1
pass1
0
user2
pass2
1
user3
pass3
2
user4
pass4
3
回答by xuhdev
It means the program crashed before it exited. You need to debug the program. For example, you need to check whether the file is successfully opened after fopen.
这意味着程序在退出之前崩溃了。您需要调试程序。例如,您需要检查文件是否在fopen.
回答by einpoklum
TL;DR: Your program tried to make a memory access to a place it had no permissions for, so the operating system killed it.
TL;DR:你的程序试图对一个它没有权限的地方进行内存访问,所以操作系统杀死了它。
First: The code "139" doesn't matter, forget about the number. Your program was terminated after "getting a SIGSEGV", or a signall regarding a segmentation violation. Read about what that means here:
第一:代码“139”无关紧要,忘记数字。您的程序在“获得 SIGSEGV”或有关分段违规的信号后终止。在这里阅读这意味着什么:
What is SIGSEGV run time error in C++?
(never mind that question is about C++, same idea.)
(别介意这个问题是关于 C++,同样的想法。)
Now, why would this happen? You must be making some assumptions you shouldn't be. Looking at your code, it might be:
现在,为什么会发生这种情况?你一定做了一些你不应该做的假设。查看您的代码,它可能是:
- Reading a very long string from the file which exceeds the bounds of the
loginInformationarray - and perhaps even the bounds of the memory region allocated to your program overall. - Scanning from an invalid-state/uninitialized/null file descriptor, as in @xuhdev's answer
- (Unlikely/impossible) Ignoring some error generated by one of the
fscanf()calls (you need to checkerrnoif a scan failed).
- 从文件中读取一个非常长的字符串,它超出了
loginInformation数组的边界——甚至可能是整个分配给程序的内存区域的边界。 - 从无效状态/未初始化/空文件描述符扫描,如@xuhdev 的回答
- (不太可能/不可能)忽略其中一个
fscanf()调用生成的错误(您需要检查errno扫描是否失败)。
I think that covers it although maybe I missed something. Instead of speculating you can actually check what happened using a debugger on the core dump:
我认为这涵盖了它,尽管我可能错过了一些东西。您可以使用核心转储上的调试器实际检查发生的情况,而不是推测:
回答by BAMF4bacon
SIGSEV are not always throwndue to a root cause of memory access problems...
由于内存访问问题的根本原因,SIGSEV 并不总是被抛出......
Perl throws a 139 on Unix usually because of file I/O. You might have accidentally deleted your input files.
Perl 在 Unix 上抛出 139 通常是因为文件 I/O。您可能不小心删除了输入文件。

