ディレクトリ全体をスキャンして.worファイルを探し、そこから情報を抽出するコードがあります。5つの.worファイルがあるディレクトリでファイルを実行すると、完全に機能します。しかし、70以上の.worファイルがあるディレクトリで実行しようとすると、「Memeryfault(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;
}