我的代码在这里假设扫描所有.wor文件的目录,并从每个文件中提取信息到新的.csv文件。
但是,它只适用于一个目录(我正在编写此c程序的目录,其中包含2个.wor文件和一些文本文件)。当我尝试输入一个只有3个.wor文件的不同目录时,它会给我一个错误消息,我指定它在文件== NULL时显示;从这一行:
if (fs == NULL)
{
puts ("Cannot open source file");
return 1;
}
如果我一起删除这个if语句,它会显示“总线错误(核心转储)”错误。
我不确定发生了什么,有什么我不在这里做的吗?
这是我的完整代码:
#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;
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_Prog,Software,DUT_board_id,Corl box\n");
/* Open Directory*/
if (NULL == (FD = opendir ("/home/iselabs/dwang/pinscale/workorder/practice")))
{
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 */
size_t len = strlen(in_file->d_name);
if (len >= 4 && memcmp(in_file->d_name + len - 4, ".wor", 4) == 0) /* checks if file ends with .wor */
{
FILE * fs = fopen(in_file->d_name, "r"); /* open file to read */
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){
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] = ' ';
}
else if (strcmp(result,"DUT_board_id")==1){
corlbox = "N/A";
}
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] = ' ';
}
else if (strcmp(result,"box")==1){
corlbox = "N/A";
}
result = strtok(NULL,delims);
}
}
fprintf (ft, "%s,%s,%s,%s,%s,%s,%s\n", in_file->d_name, customer, device, testprog, software, dutboardid, corlbox);
printf(in_file->d_name);
printf("\n");
fclose (fs) ;
c++;
}
}
printf("Total Workorders Found: %d \n", c);
fclose ( ft ) ;
return 0;
}
答案 0 :(得分:2)
您需要将目录名称与您尝试打开的文件名相结合,或者在尝试打开文件之前调用chdir()
。
你可以malloc
一个缓冲区,它基于目录名的总长度加上文件名,加上路径分隔符(/
)的额外字符,还有一个用于空终止符。
然后使用sprintf( buffer, "%s/%s", directory, filename);
获取文件的完整路径,然后尝试打开它。
在您的错误字符串中添加printf
,其中包含您尝试打开的文件的名称,以帮助您进行调试。