LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Store data (langage C) (https://www.linuxquestions.org/questions/programming-9/store-data-langage-c-305608/)

os2 03-24-2005 02:58 PM

Store data (langage C)
 
hi

this is a function to retrive text file

Code:

int searchFile(char *directory, char *ext)
{
  DIR *dirh;
  struct dirent *dirinfo;
  char *file_ext;
  int num = 0;
 
  dirh = opendir(directory);
 
  while(dirh)
  {
    if((dirinfo = readdir(dirh)) != NULL)
    {
      if((file_ext = strrchr(dirinfo->d_name, '.')) != NULL)
        file_ext++;
      else
        file_ext = dirinfo->d_name;
     
      if(!strcmp(file_ext, ext))
      {
          printf("%s\n", dirinfo->d_name);
          //store file name
      }
      num++;
    }
    else break;
  }
 
  closedir(dirh);
 
  return num;
}

i search way to use dirinfo->d_name outside this function

somebody could post code to help me to do it?


thanks

Mara 03-24-2005 03:51 PM

You can name dirinfo a global variable or add third parameter to the function:
Code:

int searchFile(char *directory, char *ext,struct dirent **dirinfo)
It's a pointer to pointer, so you need to access it a bit differently, but this allows you to use dirinfo parameter to get the value from the function.

itsme86 03-24-2005 04:07 PM

I would do it like this:
Code:

int searchFile(char *directory, char *ext,char *buf)
{
  DIR *dirh;
  struct dirent *dirinfo;
  char *file_ext;
  int num = 0;
 
  dirh = opendir(directory);
 
  while(dirh)
  {
    if((dirinfo = readdir(dirh)) != NULL)
    {
      if((file_ext = strrchr(dirinfo->d_name, '.')) != NULL)
        file_ext++;
      else
        file_ext = dirinfo->d_name;
     
      if(!strcmp(file_ext, ext))
      {
          printf("%s\n", dirinfo->d_name);
          strcpy(buf, dirinfo->d_name);
      }
      num++;
    }
    else break;
  }
 
  closedir(dirh);
 
  return num;
}

And then just create an array and pass that to the function when you call it. When the function returns, that array will be filled with the filename.

TheLinuxDuck 03-24-2005 04:19 PM

Since dirent->d_name is only a string (char[256] to be specific), and if that's all you want from the struct, then I'd suggest either doing Mara's second option, but with a string:
Code:

int searchFile(char *directory, char *ext, char *dirinfo)
or, pass the string value out of the function on exit:
Code:

#include <stdio.h>

char *searchFile(const char *directory, const char *ext)
{
  char *dname;

  if((dname = (char *)malloc(sizeof(char) * 40)) != NULL) {
    strncpy(dname, "Banana", 30);
    return dname;
  }
  else {
    perror("Couldn't init dname");
  }
  return NULL;
}

int main(void)
{
  char *filename;

  filename = searchFile("/funky/monkey", "jpg");
  if(filename != NULL) {
    printf("filename: %s\n", filename);
    free(filename);
  }

  return 0;
}

Of course, the latter requires a little more work to do. (=

TheLinuxDuck 03-24-2005 04:23 PM

Quote:

Originally posted by itsme86
Code:

          strcpy(buf, dirinfo->d_name);

Be careful here! How much storage space is allocated to buf?
What if d_name is longer than buf has storage space?

strncpy is always a better choice.


All times are GMT -5. The time now is 11:28 PM.