逐行读取C语言并存储在数组中
我试图从文件列表中读取文件名,并将这些名称存储在filenames[]数组中逐行读取C语言并存储在数组中,c,linux,C,Linux,我试图从文件列表中读取文件名,并将这些名称存储在filenames[]数组中 const int NUMBER_OF_FILES = 100;//here please define number of files listed in char* filenames [NUMBER_OF_FILES];//will contain all the file names in the list provided in List_of_input_files.txt int counter=0;
const int NUMBER_OF_FILES = 100;//here please define number of files listed in
char* filenames [NUMBER_OF_FILES];//will contain all the file names in the list provided in List_of_input_files.txt
int counter=0;
FILE *inputfilelist = fopen("List_of_input_files.txt", "r");
char line[256];
while(fgets(line, sizeof(line), inputfilelist)){
filenames[counter]=line;
printf("%s counter: %d\n\n\n", filenames[counter], counter);
counter++;
}
//**************PROBLEM IS HERE***************
printf("\n\n\n\n%s Counter: 3 ", filenames[2]);// this is only to test what is inside this filename stored
fclose(inputfilelist);
输出为:
File1.txt
counter: 0
File2.gif
counter: 1
File3.bat
counter: 2
File4.xml
counter: 3
File5
counter: 4
File6.7z
counter: 5
File7.xlsx
counter: 6
File7.xlsx
Counter : 3
问题是最后一个文件名应该是File3.bat,但不知为什么它不是。
它始终显示行中的最后一个文件,即File7.xlsx
也许这是一个愚蠢的错误,我正在做,但没有看到清楚或什么。我是c的新手,所以我不确定。请帮忙
它得到了改进,并且添加了请求的内容。这一行
filenames[counter]=line;
不做你想做的事;它将行
缓冲区的地址复制到文件名[计数器]
;您所有的文件名[i]
都指向行
,因此当您打印文件名[2]
时,它将打印出最后读入行的内容
如果要将行
的内容复制到文件名[计数器]
,则需要使用malloc
或calloc
分配内存以保存副本,然后使用strcpy
复制内容(如果可用,则使用strdup
):
或
完成后,您需要释放每个文件名[i]
或者,您可以将文件名
数组声明为字符的2D数组
:
char filenames[NUMBER_OF_FILES][256];
并直接读取到阵列中:
fgets(filenames[counter], sizeof(filenames[counter]), inputfilelist))
您必须分配内存来存储多个文件名
const int NUMBER_OF_FILES = 100;//here please define number of files listed in
const int MAX_FILE_NAME_SIZE = 50;
char filenames[NUMBER_OF_FILES][MAX_FILE_NAME_SIZE];//will contain all the file names in the list provided in List_of_input_files.txt
int counter=0;
FILE *inputfilelist = fopen("List_of_input_files.txt", "r");
char line[256];
while(fgets(line, sizeof(line), inputfilelist))
{
//filenames[counter]=line;
strcpy(filenames[counter],line);
printf("%s counter: %d\n\n", filenames[counter], counter);
counter++;
}
//**************PROBLEM IS HERE***************
printf("\n\n\n%s Counter: 3 ", filenames[2]);// this is only to test what is inside this filename stored
fclose(inputfilelist);
放置代码时,应检查缩进的一致性和可读性。张贴List\u of\u input\u files.txt的内容,您正在将所有数据读取到同一数组中,从而覆盖以前的内容。可能不是你想做的。@ITguy-发布输入文件。此外,每次将fread放入linebuffer时,都会覆盖以前的内容<代码>文件名
是指针数组。所有这些指针都指向lineBuffer。同样,lineBuffer指向的数据会随着对fread的每次调用而更改。您应该在使用strdup
函数从文件中读取后复制每个字符串。将数组的元素设置为每次在lineBuffer上调用的此函数的返回值。;)@实际上,它们是输入文件中的换行符。打印出由单引号括起的字符串,很快就会发现这些行是从哪里来的。\n为什么将行读取到256大小的缓冲区中,却将它们复制到50大小的缓冲区中?建议字符行[最大文件名大小]代码>。可能还想在fgets()
@chux之后去掉尾随的'\n'
,我通常不会在我提供的答案中加入所有的错误检查。我相信给予最少的工作代码。我把它们作为练习留给提问者。顺便说一句,我同意,代码中还有很多问题。如果我必须将其用于个人用途,我不会按原样使用:)
fgets(filenames[counter], sizeof(filenames[counter]), inputfilelist))
const int NUMBER_OF_FILES = 100;//here please define number of files listed in
const int MAX_FILE_NAME_SIZE = 50;
char filenames[NUMBER_OF_FILES][MAX_FILE_NAME_SIZE];//will contain all the file names in the list provided in List_of_input_files.txt
int counter=0;
FILE *inputfilelist = fopen("List_of_input_files.txt", "r");
char line[256];
while(fgets(line, sizeof(line), inputfilelist))
{
//filenames[counter]=line;
strcpy(filenames[counter],line);
printf("%s counter: %d\n\n", filenames[counter], counter);
counter++;
}
//**************PROBLEM IS HERE***************
printf("\n\n\n%s Counter: 3 ", filenames[2]);// this is only to test what is inside this filename stored
fclose(inputfilelist);