LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-24-2005, 02:58 PM   #1
os2
Member
 
Registered: Dec 2003
Location: Canada
Distribution: openSUSE Tumbleweed
Posts: 209

Rep: Reputation: 30
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
 
Old 03-24-2005, 03:51 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
Old 03-24-2005, 04:07 PM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
 
Old 03-24-2005, 04:19 PM   #4
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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. (=
 
Old 03-24-2005, 04:23 PM   #5
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
store data in / bruse Linux - Newbie 2 05-01-2005 11:28 AM
Store Data in Flash subrahmanyam Linux - Newbie 1 11-08-2004 12:18 PM
Store data in Flash subrahmanyam Linux - General 1 11-08-2004 12:07 PM
Store data in Flash subrahmanyam Programming 1 11-08-2004 12:06 PM
Store data in Flash subrahmanyam Linux - Software 1 11-08-2004 09:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration