如何通读c中的文件列表

how to read through a list of files in c

我正在尝试使用 for 循环读取 C 中的文件列表,但它只读取第一个文件,然后存在 for 循环,我不确定为什么。我是 C 的新手,所以我可能犯了一个愚蠢的错误,但我已经坚持了一段时间了。这是我的代码。

#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#define MAXCHAR 1000


char* fileNames[] = {"file1.csv", "file2.csv"};
FILE* fp;                 //pointer to file

int main(){

  for(int i = 0; i < sizeof(fileNames[i])/sizeof(char *); i++){
    fp = fopen(fileNames[i],"r");
    char line[MAXCHAR];

    //If file can't be read
    if(fp == NULL){
      printf("Could not open file %s","latitude.csv");
      return 0;
    }

    printf("Reading file %s\n",fileNames[i]);
    while(fgets(line, MAXCHAR, fp) != NULL){

    }
    fclose(fp);

  }
  return 0;
}

您的大小计算中有一个杂散数组下标,这导致您的循环仅在 1 次迭代后终止:sizeof(fileNames[i])/sizeof(char *)

将其更改为:sizeof(fileNames)/sizeof(fileNames[0])