-1

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

    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);

           }`

`

4

1 回答 1

0

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

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

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

于 2013-06-24T12:24:13.473 回答