从文件夹或目录中读取所有文件

时间:2013-06-24 10:20:48

标签: c

嘿,我刚接触编程..我有一个代码,其中我读取目录中的文件..我能够单独阅读它们但我需要提供目录或文件夹名称,并能够读取目录中的所有文件或提到的文件夹。请帮助我这个

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

           }`

`

1 个答案:

答案 0 :(得分:0)

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

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

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