-1

hey im new to programing.. i have a code wherein im read files in a directory.. i m able to read them individually but i need to give a directory or a folder name and be able to read all files in the directory or folder mentioned. kindly help me with this

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

           }`

`

  • 3
    Take a look at this example: http://stackoverflow.com/a/11737506/1758762. – Leo Chapiro Jun 24 '13 at 10:22
  • hey!thanks a ton for the reply , i refered to the code u posted but it dint help me resolve my issue, i need to read say if thier are twenty files in a folderr , i need to read all of them one by one cud u pls help me wid this. – user2515734 Jun 25 '13 at 06:41

1 Answers1

0

If implementing under windows just read use dirent.h

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

You're gonna want to check the readdir function and check the example. Use it to get the name of the files than apply the rest of the process for each file,

Avner Solomon
  • 1,486
  • 11
  • 17
  • hey thanks for the reply! am not using windows, using linux . yeah m able to list the names of the files in the directory, how do i get the code to read all of them one by one – user2515734 Jun 25 '13 at 05:12
  • just use the normal code to read a file since now you know their names. Make a function that gets a path as argument and reads a file. For example let's say the folder is "dir" and you get "file1" and "file2" . For each of them you should call your_read_function("dir/file1") and your_read_function("dir/file2"), in which you fopen() the file , read it with fgets or fscanf or binary read (according to your needs and fclose(). – Avner Solomon Jun 25 '13 at 06:07
  • hey avner! thanks for the reply. but it b tedious if i had to give the names of each file. this folder has abt 20 files which has to be read one by one. how do i do that? pls help – user2515734 Jun 25 '13 at 06:33
  • im very new to this wud really like some help, i jus need a code which wud read all 20 files in a folder one by one – user2515734 Jun 25 '13 at 06:39
  • You don't have to give the names yourself , that was an example. You just pass each file for the function. Let's say you were showing the files via printf("%s\n",filename); int a loop. Instead of that create a path via concat/sprintf and call your func `sprintf(path_to_file,"%s/%s",dir_name,file_name); your_file_read_func(path_to_file); ` – Avner Solomon Jun 25 '13 at 06:40
  • i get the idea, but m having difficulty in implementing, could you tell me as where i shud and how i should implement this in the code above. thanks in advance. – user2515734 Jun 25 '13 at 08:09
  • here is a read file line by line http://www.daniweb.com/software-development/c/code/216411/reading-a-file-line-by-line – Avner Solomon Jun 25 '13 at 08:46
  • u must do that in your function .. I'm at work now .. gonna try and help later – Avner Solomon Jun 25 '13 at 08:46
  • i got the concept of sprintf how do write a file of a pattern like this : kerg3460.03o – user2515734 Jun 27 '13 at 08:26
  • `sprintf(filename,"%s%d.%s","kerg",3460,"03o");` ? I don't get you're formatting ... if you could explain it, I could help. Also when listing the directory it will give you the filenames .... – Avner Solomon Jun 27 '13 at 08:55
  • i meant, i have file like this 4 letter alphabet followed by 4 digits and "." followed by 2 digits and one alphabet. – user2515734 Jun 27 '13 at 09:28
  • just use string/number/chars and limit them to the number of chars you need ... with some if conditionals – Avner Solomon Jun 27 '13 at 10:18
  • could u tell me as how exactly i shud do? please don mind me askin this, im very inexperienced and new to programming , also i m gettin segmentation fault. if its not a trouble for u il send the code and cud u pls tell me wher i m goin wrong – user2515734 Jun 27 '13 at 10:22
  • If you get a segmentation fault just find where it is and post another question ... this is no longer related to the question anymore. + is not clear if you have a certain rule to generate those names or those are just random chars. If you need random chars use srand() + rand() http://www.cplusplus.com/reference/cstdlib/rand/ `rand() % ('z'-'a') + 'a'` should generate a random lowercase letter – Avner Solomon Jun 27 '13 at 10:34