Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
逐行读取C语言并存储在数组中_C_Linux - Fatal编程技术网

逐行读取C语言并存储在数组中

逐行读取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;

我试图从文件列表中读取文件名,并将这些名称存储在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;
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);