LinuxQuestions.org
Help answer threads with 0 replies.
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-25-2005, 09:20 AM   #1
os2
Member
 
Registered: Dec 2003
Location: Canada
Distribution: openSUSE Tumbleweed
Posts: 209

Rep: Reputation: 30
Problem to acess to a dynamic string


hi

in my main i do:

Code:
char *led_line=NULL;
searchFile(mnt_dir_led, "txt", led_line);
in my searchFile i do:
Code:
int searchFile(char *directory, char *ext, char *led_line)
{
  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))
      {
        analyzeFilename(dirinfo->d_name, strlen(dirinfo->d_name), led_line);
      }
      num++;
    }
    else break;
  }
  closedir(dirh);  
  return num;
}
in my analyzeFileName i do:

Code:
void analyzeFilename(char *filename, int size, char *led_line)
{
    readFile(tmp_mnt_dir_led, size, mnt_dir_led, filename, led_line);
    printf("led_line: %s\n",led_line);
in my readFile i do:
Code:
void readFile(char *tmp_dir_led, int size, char *directory, char *filename, char *led_line)
{
    FILE *fp;
    char line[LINE_MAX];
    snprintf(tmp_mnt_dir_led, sizeof(tmp_mnt_dir_led),"%s/%s", directory,filename);        
    fp = fopen(tmp_mnt_dir_led, "r");
    if(fp!=NULL)
    {
        fgets(line, LINE_MAX, fp);
        if(led_line!=NULL)
             free(led_line);
        led_line=(char *) malloc (sizeof(line));
        strcpy(led_line,line);
    }
}
why in my analyzeFilename function, after the call function to readFile i get NULL when i printf led_line?

i want to do some action to led_line after the readFile... but i get NULL

how to resolve that?

thanks
 
Old 03-25-2005, 10:19 AM   #2
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
The first and best thing you can do is error check the malloc call:
Code:
void someFunction(void)
{
  char * ptr;

  ptr = (char *)malloc(sizeof(char) * 100);
  if(ptr == NULL) {
    perror("Malloc on ptr failed");
    return;
  }

  //  something fancy done here

  free(ptr);
}
The other thing I noticed is that, if the file open fails, the code does not produce any kind of error message. That would be another good idea.
 
  


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
Problem with copying string alltime Programming 5 04-13-2005 09:05 PM
Acess Point Problem glaz Linux - Wireless Networking 1 01-25-2005 04:06 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
dynamic ip and mail problem kev82 Linux - Networking 7 08-08-2003 02:37 PM
Dynamic IP problem .... fend88 Linux - Networking 3 05-15-2002 12:46 AM

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

All times are GMT -5. The time now is 05:53 AM.

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