【问题标题】:read all files from a folder or directory从文件夹或目录中读取所有文件
【发布时间】:2013-06-20 19:43:16
【问题描述】:

嘿,我是编程新手.. 我有一个代码,其中我读取目录中的文件.. 我能够单独读取它们,但我需要提供目录或文件夹名称,并且能够读取目录中的所有文件或提到的文件夹。请帮我解决这个问题

    int main(int argc, char **argv)
    {

     char rid[15];
     char buffer[100]; 
     char a[100]; 
     char b[100]; 
     DIR *h;
     struct dirent *dir;
     h = opendir(".");
     if (h)
     {
       while ((dir = readdir(h)) != NULL)
        {
          FILE *file = fopen (dir->d_name , "r" );
           printf("%s\n", dir->d_name);
        }

        closedir(h);
      }
      char c[100];

      char d[100]; 
      char temp[250]="";

      char *token;
      int comchk = 0;
      int lino ;.
      char ch[20],
      ch1[20];
      char value[250],
      value1[250], 
      value2[250],
      value3[250];
      char query[250]="";
      int i;
      static const char filename[10];
      MYSQL *con = mysql_init(NULL);
      if (con == NULL) 
      {
       fprintf(stderr, "%s\n", mysql_error(con));
         exit(1);
      } 
      if (mysql_real_connect(con, "localhost", "madhu", "cmmacsgps", "rinex", 0,        0) == NULL) 

      {

       finish_with_error(con);

      }
      if (mysql_query(con, "INSERT INTO rinex1     VALUES('','no','no','no','no','no','no','no','no','no','no','no','no')"))
      {    
      finish_with_error(con);


      }

      int nrid = mysql_insert_id(con);
       sprintf(rid, "%d", nrid);
       printf("enter filename\n");
       scanf("%s",filename);
       FILE *file = fopen ( filename, "r" );
       if ( file != NULL )
        {
         char line [250]; /* or other suitable maximum line size */
         char line1 [250];
          char *token1;
          char *token2;
          int tabno=0;

         while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
         {
         lino =lino+1;
         strcpy(line1,line);

          substring(60, 80, line1, a, sizeof a);
          substring(0, 60, line1, b, sizeof b);
          strcpy(c,a);
          }
         fclose ( file );
          }
          mysql_close(con);

          exit(0);

           }`

`

【问题讨论】:

  • 看看这个例子:stackoverflow.com/a/11737506/1758762.
  • 嘿!非常感谢您的回复,我参考了您发布的代码,但它有助于解决我的问题,我需要阅读说如果文件夹中有 20 个文件,我需要请一一阅读所有这些内容,请帮助我。

标签: c


【解决方案1】:

如果在 windows 下实现,只需阅读使用 dirent.h

http://pubs.opengroup.org/onlinepubs/007908799/xsh/dirent.h.html

您将要检查 readdir 函数并检查示例。 使用它来获取文件的名称,而不是为每个文件应用其余的过程,

【讨论】:

  • 您好,谢谢您的回复!我不使用 windows,使用 linux 。是的,我能够列出目录中文件的名称,我如何让代码一个一个地读取它们
  • 只需使用普通代码读取文件,因为现在您知道它们的名称了。创建一个获取路径作为参数并读取文件的函数。例如,假设文件夹是 "dir" 并且你得到 "file1" 和 "file2" 。对于它们中的每一个,您应该调用 your_read_function("dir/file1") 和 your_read_function("dir/file2"),在其中您 fopen() 文件,使用 fgets 或 fscanf 或二进制读取(根据您的需要和 fclose ()。
  • 嘿,阿夫纳!谢谢回复。但是如果我必须给出每个文件的名称,那就太乏味了。这个文件夹有大约 20 个文件,必须一一阅读。我怎么做?请帮助
  • 我对这个 wud 非常陌生,真的很想得到一些帮助,我只需要一个代码,它可以一个一个地读取文件夹中的所有 20 个文件
  • 你不必自己给出名字,这是一个例子。您只需为函数传递每个文件。假设您通过 printf("%s\n",filename); 显示文件。进入一个循环。而不是通过 concat/sprintf 创建路径并调用您的 func sprintf(path_to_file,"%s/%s",dir_name,file_name); your_file_read_func(path_to_file);
猜你喜欢
  • 2013-02-28
  • 2018-07-04
  • 1970-01-01
  • 2018-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-11
相关资源
最近更新 更多