GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Mar-2005, 09:19
collinm collinm is offline
New Member
 
Join Date: Mar 2005
Posts: 20
collinm is on a distinguished road

Problem to acess to a dynamic string


hi

in my main i do:

CPP / C++ / C Code:
char *led_line=NULL;
searchFile(mnt_dir_led, "txt", led_line);

in my searchFile i do:
CPP / C++ / C 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:

CPP / C++ / C 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:
CPP / C++ / C 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
Last edited by LuciWiz : 25-Mar-2005 at 09:23. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 25-Mar-2005, 10:21
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by collinm
hi

in my main i do:

in my analyzeFileName i do:

CPP / C++ / C 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:
CPP / C++ / C Code:
void readFile(char *tmp_dir_led, int size, char *directory, char *filename, char *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

You pass led_line (a pointer to char) to readFile(). Inside readFile(), you set the value of readFile()'s local copy of led_line to the allocated storage. This does not affect the value of led_line in the calling program.

One way around this is to pass the address of led_line (therefore a pointer to a pointer to char).

An example:

CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void print_something(char *);

int main()
{
  int do_something(char **);

  char *led_line = NULL;

  if (!do_something(&led_line)) {
    printf("do_something() failed. Terminating program\n");
    return 1;
  }

  printf("\nIn main().                led_line = <%s>\n", led_line);
  printf("\nIn main().                Calling print_something()\n");
  print_something(led_line);

  if (led_line) {
    free(led_line);
  }

  return 0;
}

int do_something(char **line)
{
  int length;
  char *this_is_it = "This is from do_something";
  length = strlen(this_is_it);

  *line = malloc(length + 1); /* an extra byte for the terminating zero byte */
  if (!*line) {
    printf("Error: malloc() couldn't allocate %d bytes\n", length+1);
    return 0; /* error return */
  }
  else {
    strcpy(*line, this_is_it);
    printf("\nIn do_something().        Calling print_something)()\n");
    print_something(*line);
    return 1; /*OK return */
  }
}

void print_something(char *s)
{
  printf("\nIn print_something().     s = <%s>\n", s);
}

Regards,

Dave
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a .html file, check that file for links salemite C Programming Language 10 17-Jan-2008 08:56
Problem with string and serial port collinm C Programming Language 13 25-Mar-2005 08:39
Please help! Dynamic binary tree problem robsmith C Programming Language 3 15-Mar-2005 22:20
C++ string from file small_ticket C++ Forum 3 05-Jan-2005 08:29
problem reading to a dynamic array noamfrie C Programming Language 9 02-Jan-2005 19:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:12.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.