【发布时间】:2015-08-29 16:49:16
【问题描述】:
我正在尝试处理一些文件。文件路径存储在 txt 文件中。每行有 4 个文件名,每个文件名用空格分隔。我想将一行读入缓冲区,将缓冲区划分为这 4 个文件路径,然后处理它们。完成后,我想对下一行的文件路径执行相同的操作。
我有以下代码来处理文件名:
int i, j, check, fileidx, filenum;
char buf[100000], *testfile;
char filename_out[500], filename_mess[500], filename_ref[500], filename_dark[500];
filenum = getFileNumber("filelist.txt"); //get number of lines in the txt file
filelist = fopen("filelist.txt","r");
for (fileidx=0; fileidx<filenum; fileidx++){
memset(&filename_mess[0], 0, sizeof(filename_mess)); //clear filenames
memset(&filename_dark[0], 0, sizeof(filename_dark));
memset(&filename_ref[0], 0, sizeof(filename_ref));
memset(&filename_out[0], 0, sizeof(filename_out));
memset(&buf[0],0,sizeof(buf));
for (check = 0;check<fileidx+1;check++){ //up to the current file index
testfile = fgets(buf,100000,filelist); //get each line into the buffer
if ((testfile == NULL)&(check == fileidx)){ //if at the correct file index and line is empty
printf("Error in filelist (line %i is empty!)",check+1);
goto fileerror;
};
};
if (fileidx==filenum-1)
sprintf(buf,"%s\0",buf); //desperate try - not helping
if ((buf[0] == '\n') | (buf[0] == ' ') | (buf[0] == EOF)){
printf("Error in filelist (line %i)!",fileidx+1);
goto fileerror;
};
i=0;
while (buf[i]!=' '){ //while not hitting the first space
if (buf[i] == '\n'){ //if a filename is missing, the end of the line is found too early
printf("Error in filelist (line %i)! Element missing!",fileidx+1);
goto fileerror; //goto the next file
};
filename_mess[i] = buf[i]; //copy the buffer to the first filename array
i++;
};
i++; //skip the space between the filepaths
j = i; //save start of second filepath
while (buf[i]!=' '){ //repeat the process for second filename
if (buf[i] == '\n'){
printf("Error in filelist (line %i)! Element missing!",fileidx+1);
goto fileerror;
};
filename_ref[i-j] = buf[i];
i++;
};
i++;
j = i;
while (buf[i]!=' '){ //repeat process for third filename
if (buf[i] == '\n'){
printf("Error in filelist (line %i)! Element missing!",fileidx+1);
goto fileerror;
};
filename_dark[i-j] = buf[i];
i++;
};
i++;
j = i;
while ((buf[i]!='\n') & (buf[i]!= EOF) & (buf[i]!=' ')){ //we're at the last element, check for end of line or EOF here, too
if (buf[i] == ' '){
printf("Error in filelist (line %i)! Too many elements!",fileidx+1);
goto fileerror;
};
filename_out[i-j] = buf[i];
i++;
};
//read the files, process them
};
此代码对除最后一行之外的所有行都运行良好。在最后一行中,只有最后一个元素存储在“filename_out”中,其他元素(mess、ref 和 dark)保持为空。缓冲区“buf”看起来不错,但是(使用printf("%s",buf) 检查它会显示预期的行)。当我在文本文件的最后一行下方添加一个空行时,读取工作正常。我还尝试使用sprintf(buf,"%s\n",buf) 每次都将这个(看似必要的)字符添加到缓冲区 - 没有用。
澄清一下:读取一个包含
的txt文件R1E1 R1E2 R1E3 R1E4
R2E1 R2E2 R2E3 R2E4
会给我:
File1:
filename_mess = R1E1
filename_ref = R1E2
filename_dark = R1E3
filename_out = R1E4
File2:
filename_mess =
filename_ref =
filename_dark =
filename_out = R2E4
虽然应该是:
File1:
filename_mess = R1E1
filename_ref = R1E2
filename_dark = R1E3
filename_out = R1E4
File2:
filename_mess = R2E1
filename_ref = R2E2
filename_dark = R2E3
filename_out = R2E4
在文本文件末尾添加一个空行将返回:
File1:
filename_mess = R1E1
filename_ref = R1E2
filename_dark = R1E3
filename_out = R1E4
File2:
filename_mess = R2E1
filename_ref = R2E2
filename_dark = R2E3
filename_out = R2E4
File3:
*Error Message*
编辑:删除了一些错误。
【问题讨论】:
-
在这段代码中的许多不正确/有问题的事情中,
while (buf[i]!=' ')-buf[i]是char*,您将它与 char 文字进行比较。最大化您的编译器警告级别,因为其中标记了 许多。它至少看起来像buf应该是char数组,而不是char*数组。 -
这里的代码有错误,抱歉。它实际上是
char。 -
发布代码时,请将真实代码还原为minimum, complete, and verifiable example后复制/粘贴,以重现您的问题。复制/粘贴 = 没有错别字。
-
而且由于您不需要逐行保留 fname(此代码当然不需要),因此可以将其设为 considerably simpler。