【发布时间】:2016-09-16 17:34:30
【问题描述】:
我尝试多次而不是一次读取文件。 在尝试时,我遇到了很多分段错误。带有 while 循环的程序部分如下所示:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
/* General use buffer */
#define STRLEN 8196
char string[STRLEN];
int lines = 1024;
char **line;
int linemax;
int longest=0;
int main(){
int len,i;
int zwei = 1;
FILE * fp;
char *s;
int debug = 0;
line=(char **)malloc(sizeof(char *) * 1024);
do{
if ( (fp = fopen("rhel7_160731_0606.nmon", "r")) == NULL) {
perror("failed to open file");
perror("fopen");
exit(75);
}
printf("where is the problem1,3\n");
for (i = 0; fgets(string, STRLEN, fp) != NULL; i++) {
if (i >= lines) {
lines += 1024;
line = (char **)realloc((void *)line, sizeof(char *) * lines);
}
if (string[strlen(string)-1] == '\n')
string[strlen(string)-1] = 0;
if (string[strlen(string)-1] == '\r')
string[strlen(string)-1] = 0;
if (string[strlen(string)-1] == ' ')
string[strlen(string)-1] = 0;
if (string[strlen(string)-1] == ',')
string[strlen(string)-1] = 0;
len = strlen(string) + 1;
if (len > longest)
longest = len;
s = malloc(len);
strcpy(s, string);
line[i] = (char *)s;
}
linemax = i;
lines = i;
if (debug)
for (i = 0; i < linemax; i++)
printf("line %d lastline %s\n", i, line[i-1]);
zwei++;
}while(zwei<4);
return 0;
}
它什么都不挂起或以分段错误结束。
【问题讨论】:
-
你的调试器说什么?
-
程序收到信号SIGSEGV,分段错误。 0x00000000004009df in main () at test.c:53 53 line[i] = (char *)s;
-
那是哪个调试器?
-
所以现在会发生这种情况:#0 __lll_lock_wait_private () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:95 #1 0x00007ffff7a8924b in _IO_fgets (buf=0xeb2d50 "\ 002", n=8196, fp=0xea1c40) at iofgets.c:50 #2 0x000000000040a7f9 in main (argc=8, argv=0x7fffffffdf68) 它运行了两次然后出现问题。是缓冲区溢出吗?
-
} while(zwei>4);从来都不是真的,所以它不能循环两次,这不是原因,但我想知道还有多少是粗心的。
标签: c linux while-loop fopen fgets