我有一个代码,它扫描整个目录中的.wor文件,并从中提取信息。当我在具有5个.wor文件的目录上运行该文件时,它完美地工作。但是当我尝试在70多个.wor文件的目录上运行它时,它给了我一个“Memery错误(coredump”错误。
所以我尝试用gdb调试代码,这就是我得到的:
Program received signal SIGSEGV, Segmentation fault.
0x7af9fcbc in _sbrk () from /usr/lib/libc.2
(gdb) bt
#0 0x7af9fcbc in _sbrk () from /usr/lib/libc.2
#1 0x7af9db7c in _sigfillset () from /usr/lib/libc.2
#2 0x7af9bfe8 in __thread_callback_np () from /usr/lib/libc.2
#3 0x7afa0e64 in malloc () from /usr/lib/libc.2
#4 0x7b001160 in _findbuf () from /usr/lib/libc.2
#5 0x7afff7f4 in _filbuf () from /usr/lib/libc.2
#6 0x7afff404 in __fgets_unlocked () from /usr/lib/libc.2
#7 0x7afff5fc in fgets () from /usr/lib/libc.2
#8 0x2c14 in main (argc=1, argv=0x7dff11b4) at book2.c:99
我看了第99行,我有命令:while(fgets(buff,100,fs)!= NULL),我不知道c足以找到错误的位置,这里的任何帮助都会非常感激,提前谢谢!
这是我的代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char **argv) {
char directory[100];
char buff[100];
char delims[] = " :=";
char *result = NULL;
char *customer;
char *device;
char *testprog;
char *software;
char *dutboardid;
char *corlbox;
char *testerid;
char *temp;
char *buffer;
int i=0;
DIR * FD;
struct dirent* in_file;
int c=0;
printf ("Enter directory:");
scanf ("%s",directory);
FILE * ft = fopen ( "workorderlist.csv", "w" ) ; /* Open file to write to*/
if ( ft == NULL )
{
puts ( "Cannot open target file" ) ;
exit( 1 ) ;
}
fprintf (ft, "Work Order,Customer,Device,Test Program,Software,DUT_board_id,Corl box, Tempurature, Test Station\n");
/* Open Directory*/
if (NULL == (FD = opendir (directory)))
{
puts ("Cannot open directory");
return 1;
}
while ((in_file = readdir(FD)))
{
if (!strcmp (in_file->d_name, "."))
continue;
if (!strcmp (in_file->d_name, ".."))
continue;
/* Open files to read from */
buffer = (char*) malloc (200);
sprintf(buffer, "%s/%s", directory, in_file->d_name);
size_t len = strlen(buffer);
if (len >= 4 && memcmp(buffer + len - 4, ".wor", 4) == 0) /* checks if file ends with .wor */
{
FILE * fs = fopen(buffer, "r"); /* open file to read */
customer = "0";
device = "0";
testprog = "0";
software = "0";
dutboardid = "0";
corlbox = "0";
testerid = "0";
temp = "0";
if (fs == NULL)
{
puts ("Cannot open source file");
return 1;
}
/* Scanning each file for targeted words: */
while( fgets(buff, 100,fs) != NULL )
{
result = strtok( buff, delims );
while(result != NULL){
if((strcmp(result,"Customer")==0)){
result = strtok(NULL,delims);
customer = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(customer, result);
for(i=0;i<strlen(customer)+1;i++){ if(customer[i] == '\n') break; }
customer[i] = ' ';
}
if((strcmp(result,"name")==0)){
result = strtok(NULL,delims);
customer = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(customer, result);
for(i=0;i<strlen(customer)+1;i++){ if(customer[i] == '\n') break; }
customer[i] = ' ';
}
if(strcmp(result,"device")==0){
result = strtok(NULL,delims);
device = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(device, result);
for(i=0;i<strlen(device)+1;i++){ if(device[i] == '\n') break; }
device[i] = ' ';
}
if(strcmp(result,"test_prog")==0 || strcmp(result, "testprog")==0){
result = strtok(NULL,delims);
testprog = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(testprog, result);
for(i=0;i<strlen(testprog)+1;i++){ if(testprog[i] == '\n') break; }
testprog[i] = ' ';
}
if(strcmp(result,"Rev")==0 || strcmp(result,"use")==0){
result = strtok(NULL,delims);
software = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(software, result);
for(i=0;i<strlen(software)+1;i++){ if(software[i] == '\n') break; }
software[i] = ' ';
}
if(strcmp(result,"rev")==0){
result = strtok(NULL,delims);
software = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(software, result);
for(i=0;i<strlen(software)+1;i++){ if(software[i] == '\n') break; }
software[i] = ' ';
}
if(strcmp(result,"DUT_board_id")==0){
result = strtok(NULL,delims);
dutboardid = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(dutboardid, result);
for(i=0;i<strlen(dutboardid)+1;i++){ if(dutboardid[i] == '\n') break; }
dutboardid[i] = ' ';
}
if(strcmp(result,"box")==0){
result = strtok(NULL,delims);
corlbox = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(corlbox, result);
for(i=0;i<strlen(corlbox)+1;i++){ if(corlbox[i] == '\n') break; }
corlbox[i] = ' ';
}
if(strcmp(result,"Tester_id")==0){
result = strtok(NULL,delims);
testerid = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(testerid, result);
for(i=0;i<strlen(testerid)+1;i++){ if(testerid[i] == '\n') break; }
testerid[i] = ' ';
}
if(strcmp(result,"@")==0){
result = strtok(NULL,delims);
temp = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(temp, result);
for(i=0;i<strlen(temp)+1;i++){ if(temp[i] == '\n') break; }
temp[i] = ' ';
}
result = strtok(NULL,delims);
}
}
if (strcmp(customer, "0")==0){
customer = "N/A";
}
if (strcmp(device, "0")==0){
device = "N/A";
}
if (strcmp(testprog, "0")==0){
testprog = "N/A";
}
if (strcmp(software,"0")==0){
software = "N/A";
}
if (strcmp(dutboardid, "0")==0){
dutboardid = "N/A";
}
if (strcmp(corlbox, "0")==0){
corlbox = "N/A";
}
if (strcmp(testerid, "0")==0){
testerid = "N/A";
}
if (strcmp(temp, "0")==0){
temp = "N/A";
}
fprintf (ft, "%s,%s,%s,%s,%s,%s,%s,%s,%s\n", in_file->d_name, customer, device, testprog, software, dutboardid, corlbox, temp, testerid);
printf(in_file->d_name);
printf("\n");
fclose (fs) ;
c++;
}
}
printf("Total Workorders Found: %d (Info saved to workorderlist.csv)\n", c);
fclose ( ft ) ;
return 0;
}
答案 0 :(得分:1)
内存不足。
例如,您调用malloc并将其分配给循环内的“customer”,并且似乎永远不会释放内存。你有内存泄漏,最终你的内存使用量达到了系统限制。
答案 1 :(得分:1)
如果我们采用这个例子:
customer = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(customer, result);
for(i=0;i<strlen(customer)+1;i++){ if(customer[i] == '\n') break; }
customer[i] = ' ';
它复制结果,占用strlen(result)+1
个字节。然后,您将结果复制到客户。之后,您在字符串中搜索'\ n'。如果字符串中没有'\ n'会发生什么?什么价值是i
?
我很确定问题是在某些时候,这种代码风格是在分配的内存之外写 - 因为我不确切知道你的输入是什么,我不能说哪个是这个代码的几个副本出错了,但我敢打赌这就是发生的事情。这反过来意味着堆结构本身中的一些数据被破坏,因此在某些时候调用malloc时,它会在代码中使用这些错误的数据。