not able to free up a string...

C

collinm

hi

i have this function, when i try to free up line, i got a Segmentation
fault
if i remove, free(line); the program work fine

void readFile(int fd,char *directory, char *filename[], int sizedir,
int sizefile[])
{
FILE *fp;
char *line[2];
char *tmp;
int i;
int size[2];

if ( ( tmp = malloc( sizedir + sizefile[0] + 1) ) == NULL )
exit( EXIT_FAILURE );

strcpy(tmp, directory);
strcat(tmp, "/");
strcat(tmp, filename[0]);

fp = fopen(tmp, "r");
if(fp != NULL)
{

if ( ( line[0] = malloc( LINE_MAX + 1) ) == NULL )
exit( EXIT_FAILURE );

fgets(line[0], LINE_MAX, fp);
fclose(fp);
free(tmp);

if ( ( line[1] = malloc( LINE_MAX + 1) ) == NULL )
exit( EXIT_FAILURE );

if ( ( tmp = malloc( sizedir + sizefile[1] + 1) ) == NULL )
exit( EXIT_FAILURE );

strcpy(tmp, directory);
strcat(tmp, "/");
strcat(tmp, filename[1]);

fp = fopen(tmp, "r");
if(fp != NULL)
{
fgets(line[1], LINE_MAX, fp);
size[0]= strlen(line[0]);
size[1]= strlen(line[1]);

analyzeFilename(fd, filename, line, size);
fclose(fp);
}
else
fprintf(stderr, "%s - Not able to open the file: %s\n",
strerror(errno),filename[1] );
}
else
fprintf(stderr, "%s - 1 Not able to open the file: %s\n",
strerror(errno),filename[0]);

free(tmp);
free(line);
}

any idea?

thanks
 
E

Eric Sosman

collinm said:
hi

i have this function, when i try to free up line, i got a Segmentation
fault
if i remove, free(line); the program work fine

void readFile(int fd,char *directory, char *filename[], int sizedir,
int sizefile[])
{
FILE *fp;
char *line[2];
[...]
if ( ( line[0] = malloc( LINE_MAX + 1) ) == NULL )
exit( EXIT_FAILURE );
[...]

if ( ( line[1] = malloc( LINE_MAX + 1) ) == NULL )
exit( EXIT_FAILURE );
[...]
free(line);

This is wrong because `line' does not point to storage
obtained from malloc()/calloc()/realloc(). `line[0]' and
`line[1]' point to such storage, but `line' itself is an
`auto' array.
 
J

John Valko

collinm said:
hi

i have this function, when i try to free up line, i got a Segmentation
fault
if i remove, free(line); the program work fine

void readFile(int fd,char *directory, char *filename[], int sizedir,
int sizefile[])
{
FILE *fp;
char *line[2];
char *tmp;
int i;
int size[2]; [snip]
if ( ( line[0] = malloc( LINE_MAX + 1) ) == NULL )
exit( EXIT_FAILURE );

fgets(line[0], LINE_MAX, fp);
fclose(fp);
free(tmp);

if ( ( line[1] = malloc( LINE_MAX + 1) ) == NULL )
exit( EXIT_FAILURE ); [snip]
free(tmp);
free(line);
This is your problem, you're trying to free line, which is an array of
two char pointers. Rather, I assume you meant to free the memory
allocated by malloc():
free(line[0]);
free(line[1]);
}

any idea?

thanks
Hope that helps,
John
 
M

Martin Ambuhl

collinm said:
hi

i have this function, when i try to free up line, i got a Segmentation
fault
if i remove, free(line); the program work fine

'free' is an array of pointers and cannot be freed:
char *line[2];

Further, you knew this when you allocated the memory:
if ( ( line[0] = malloc( LINE_MAX + 1) ) == NULL )
exit( EXIT_FAILURE ); and
if ( ( line[1] = malloc( LINE_MAX + 1) ) == NULL )
exit( EXIT_FAILURE );

Since you should free what you allocate, you should free(line[0]) and
free(line[1]).
 
C

collinm

Martin Ambuhl wrote:
Since you should free what you allocate, you should free(line[0]) and
free(line[1]).

ok that work fine....

but another function:

int searchFile(int fd, char *directory, char *ext)
{

DIR *dirh;
struct dirent *dirinfo;
char *file_ext;
int num = 0;
int trouve=0;
dirh = opendir(directory);
char *tmp[2];
int size[2];
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))
{
trouve++;
if( trouve == 1 ){
size[0]=strlen(dirinfo->d_name);
if ( ( tmp[0] = malloc(size[0]) ) == NULL )
exit( EXIT_FAILURE );
tmp[0] = dirinfo->d_name;
}
else
{
size[1]=strlen(dirinfo->d_name);
if ( ( tmp[1] = malloc( size[1]) ) == NULL )
exit( EXIT_FAILURE );
tmp[1] = dirinfo->d_name;

readFile(fd, directory, tmp, strlen(directory),
size);
free(tmp);
trouve==0;
}
}
num++;
}
else break;
}
closedir(dirh);
return num;
}

that work but if i remove free(tmp) and i do:

free(tmp[0]);
free(tmp[1]);

i got a Segmentation fault
 
D

David Resnick

collinm wrote:
...
if( trouve == 1 ){
size[0]=strlen(dirinfo->d_name);
if ( ( tmp[0] = malloc(size[0]) ) == NULL )
exit( EXIT_FAILURE );
tmp[0] = dirinfo->d_name;
} ...
that work but if i remove free(tmp) and i do:

free(tmp[0]);
free(tmp[1]);

i got a Segmentation fault

Look more carefully at what you are doing there. You are
allocating some memory and assigning the resulting pointer
to tmp[0]. Then leaking the memory as you assign tmp[0]
to somewhere else. Perhaps you mean to do this, note the
+1 you were also missing.

size[0] = strlen(dirinfo->d_name) + 1;
if ( ( tmp[0] = malloc(size[0]) ) == NULL )
exit( EXIT_FAILURE );
memcpy(tmp[0], dirinfo->d_name, size[0]);

-David
 
C

collinm

i checked the code with more attentions

size[0] = strlen(dirinfo->d_name) + 1;
why +1?

dirinfo->d_name already include \0, no?
 
R

Richard Tobin

collinm said:
size[0] = strlen(dirinfo->d_name) + 1;
why +1?

dirinfo->d_name already include \0, no?

strlen returns the number of characters *before* the nul.

"foo" occupies 4 chars, but strlen("foo") is 3.

-- Richard
 
C

collinm

in my main function i do:
searchFile(fd, mnt_dir_led, "txt");

my program call are:

searchFile
|-----------------readFile
|---------------------- analyzeFilename(int

i don't seem to have memory leak

i do a infinit loop on searchFile and i checked memory with top and the
memory don't grow...

i post my code to have comments, tips...

int searchFile(int fd, char *directory, char *ext)
{
DIR *dirh;
struct dirent *dirinfo;
char *file_ext;
int num = 0;
int trouve=0;
dirh = opendir(directory);
char *tmp[2];
int size[2];
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))
{
trouve++;
if( trouve == 1 ){
sleep(3);

size[0]=strlen(dirinfo->d_name) + 1;
if ( ( tmp[0] = malloc(size[0] ) ) == NULL )
exit( EXIT_FAILURE );
tmp[0] = dirinfo->d_name;
}
else
{
size[1]=strlen(dirinfo->d_name) + 1;
if ( ( tmp[1] = malloc( size[1] ) ) == NULL )
exit( EXIT_FAILURE );
tmp[1] = dirinfo->d_name;

readFile(fd, directory, tmp, strlen(directory),
size);
sleep(3);
free(tmp);
trouve==0;
}
}
num++;
}
else break;
}
closedir(dirh);
return num;
}

/* read the text file */
void readFile(int fd,char *directory, char *filename[], int sizedir,
int sizefile[])
{
FILE *fp;

char *line[2];

char *tmp;
int i;
int size[2];

if ( ( tmp = malloc( sizedir + sizefile[0] + 1) ) == NULL )
exit( EXIT_FAILURE );

strcpy(tmp, directory);
strcat(tmp, "/");
strcat(tmp, filename[0]);

fp = fopen(tmp, "r");
if(fp != NULL)
{
if ( ( line[0] = malloc( LINE_MAX) + 1) == NULL )
exit( EXIT_FAILURE );

fgets(line[0], LINE_MAX, fp);
fclose(fp);
free(tmp);

if ( ( line[1] = malloc( LINE_MAX) + 1 ) == NULL )
exit( EXIT_FAILURE );

if ( ( tmp = malloc( sizedir + sizefile[1] + 1) ) == NULL )
exit( EXIT_FAILURE );

strcpy(tmp, directory);
strcat(tmp, "/");
strcat(tmp, filename[1]);

fp = fopen(tmp, "r");
if(fp != NULL)
{
fgets(line[1], LINE_MAX, fp);
size[0]= strlen(line[0]);
size[1]= strlen(line[1]);


analyzeFilename(fd, filename, line, size);
fclose(fp);
}
else
fprintf(stderr, "%s - Not able to open the file: %s\n",
strerror(errno),filename[1] );
}
else
fprintf(stderr, "%s - Not able to open the file: %s\n",
strerror(errno),filename[0]);

free(tmp);
}


void analyzeFilename(int fd, char *filename[], char *led_line[], int
size[])
{
#ifdef DEBUG
printf("analyse\n");
#endif

char CMD_INIT[] = { '\0', '\0', '\0', '\0', '\0', '\1',
'Z', '0', '0', '\2' };
char CMD_TEXT_FILE_TYPE[ ] = { 'A' };

char CMD_FILE_LABEL = *filename[ 0 ];

char CMD_TOP[] = {'\33', '\42' };
char CMD_BOT[] = {'\33', '\46' };

char CMD_HOLD[] = { '\142' };

char CMD_TXT_RED[] = {'\34', '\61' };//61
char CMD_TXT_GREEN[] = {'\34', '\61' };//62
char CMD_TXT_YELLOW[] = {'\34', '\61' };//63

char CMD_END[] = {'\04' };

int color[2]={0,0};
int position[2]={0,0};
int mode[2]={0,0};

int i;

char *ALL, *p;

size_t len = sizeof CMD_INIT + sizeof CMD_TEXT_FILE_TYPE +
sizeof CMD_FILE_LABEL + size[0] + size[1] + sizeof
CMD_END;

char *tokenptr;
char *seperators="_";
for(i=0;i<2;i++)
{
tokenptr = strtok(filename,seperators);
while (tokenptr != NULL)
{
tokenptr = strtok(NULL,seperators);

if(tokenptr!=NULL)
{
if(strcmp(tokenptr,"L1")==0)
{
len += sizeof CMD_TOP;
position=1;
}
else if(strcmp(tokenptr,"L2")==0)
{
len += sizeof CMD_BOT;
position=2;
}
else if(strcmp(tokenptr,"HLD")==0)
{
len += sizeof CMD_HOLD;
mode = 1;
}
else if (strcmp(tokenptr,"GRN")==0)
{
len += sizeof CMD_TXT_GREEN;
color = 1;
}
else if (strcmp(tokenptr,"YEL")==0)
{
len += sizeof CMD_TXT_YELLOW;
color = 2;
}
else if (strcmp(tokenptr,"RED")==0)
{
len += sizeof CMD_TXT_RED;
color = 3;
}
}
}
}

if ( ( ALL = p = malloc( len ) ) == NULL )
exit( EXIT_FAILURE );

memcpy( p, CMD_INIT, sizeof CMD_INIT );
p += sizeof CMD_INIT;

memcpy( p, CMD_TEXT_FILE_TYPE, sizeof CMD_TEXT_FILE_TYPE );
p += sizeof CMD_TEXT_FILE_TYPE;

*p++ = CMD_FILE_LABEL;

for(i=0;i<2;i++)
{
/* text position*/
if(position==1)
{
memcpy ( p, CMD_TOP, sizeof CMD_TOP );
p += sizeof CMD_TOP;
}
else if(position==2)
{
memcpy ( p, CMD_BOT, sizeof CMD_BOT );
p += sizeof CMD_BOT;
}
/*text mode display*/
if(mode==1)
{
memcpy ( p, CMD_HOLD, sizeof CMD_HOLD );
p += sizeof CMD_HOLD;
}
/* text color*/
if(color==1)
{
memcpy( p, CMD_TXT_GREEN, sizeof CMD_TXT_GREEN );
p += sizeof CMD_TXT_GREEN;
}
else if(color==2)
{
memcpy( p, CMD_TXT_YELLOW, sizeof CMD_TXT_YELLOW );
p += sizeof CMD_TXT_YELLOW;
}
else if(color==3)
{
memcpy( p, CMD_TXT_RED, sizeof CMD_TXT_RED );
p += sizeof CMD_TXT_RED;
}
memcpy( p, led_line, size );
p += size;
}
memcpy( p, CMD_END, sizeof CMD_END );

free( ALL );
}
 
D

David Resnick

dresnick wrote
size[0] = strlen(dirinfo->d_name) + 1;
if ( ( tmp[0] = malloc(size[0]) ) == NULL )
exit( EXIT_FAILURE );
memcpy(tmp[0], dirinfo->d_name, size[0]);
ok for the +1 in the maloc... but why memcpy?

You are otherwise leaking the malloced memory. Your original
code was this:
if ( ( tmp[0] = malloc(size[0]) ) == NULL )
exit( EXIT_FAILURE );
tmp[0] = dirinfo->d_name;

This is akin to the following simplified example:

char *a = malloc(10);
a = "foo";

The address of the memory returned
by malloc is no longer known to you, it has been leaked. That
is what you do above when assigning tmp[0] to dirinfo->d_name --
you make the memory returned by the malloc no longer reachable.

Clearly you wanted tmp[0] to get a copy of dirinfo->d_name,
hence the memcpy. Perhaps you believed that assigning
tmp[0] = dirinfo->d_name copies the bytes from the d_name
into the space pointed at by tmp[0]? It does not, it just
changes tmp[0] to point at the location indicated by
dirinfo->d_name.

Your code would simpler if you implement a new function,
dupstr, that returns a malloc'd copy of a string (or dies
if it can't malloc), as you do that twice in your code.

-David
 
C

CBFalconer

collinm said:
.... snip ...

i don't seem to have memory leak
.... snip ...

size[0]=strlen(dirinfo->d_name) + 1;
if ( ( tmp[0] = malloc(size[0] ) ) == NULL )
exit( EXIT_FAILURE );
tmp[0] = dirinfo->d_name;

Then what do you call this? It was pointed out to you before.
 
C

collinm

David said:
dresnick wrote
size[0] = strlen(dirinfo->d_name) + 1;
if ( ( tmp[0] = malloc(size[0]) ) == NULL )
exit( EXIT_FAILURE );
memcpy(tmp[0], dirinfo->d_name, size[0]);
ok for the +1 in the maloc... but why memcpy?

You are otherwise leaking the malloced memory. Your original
code was this:
if ( ( tmp[0] = malloc(size[0]) ) == NULL )
exit( EXIT_FAILURE );
tmp[0] = dirinfo->d_name;

This is akin to the following simplified example:

char *a = malloc(10);
a = "foo";

The address of the memory returned
by malloc is no longer known to you, it has been leaked. That
is what you do above when assigning tmp[0] to dirinfo->d_name --
you make the memory returned by the malloc no longer reachable.

does

if ( ( tmp[0] = malloc(size[0]) ) == NULL )
exit( EXIT_FAILURE );
strcpy(tmp[0], dirinfo->d_name);

resolve this problem
Clearly you wanted tmp[0] to get a copy of dirinfo->d_name,
hence the memcpy. Perhaps you believed that assigning
tmp[0] = dirinfo->d_name copies the bytes from the d_name
into the space pointed at by tmp[0]? yes

in readFile function, when i print tmp, i get the correct value
It does not, it just
changes tmp[0] to point at the location indicated by
dirinfo->d_name.

Your code would simpler if you implement a new function,
dupstr, that returns a malloc'd copy of a string (or dies
if it can't malloc), as you do that twice in your code.

ok something like

char * function_test()
{

if ( ( tmp = malloc( sizedir + sizefile[1] + 1) ) == NULL )
exit( EXIT_FAILURE );

return tmp;
}
 
C

collinm

CBFalconer wrote:

Then what do you call this? It was pointed out to you before.

some change have been done

int searchFile(int fd, char *directory, char *ext)
{
DIR *dirh;
struct dirent *dirinfo;
char *file_ext;
int num = 0;
int trouve=0;
dirh = opendir(directory);
char *tmp[2];
int size[2];
if(dirh)
{
while ( (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))
{
trouve++;
if( trouve == 1 ){
size[0]=strlen(dirinfo->d_name) + 1;
if ( ( tmp[0] = malloc(size[0] ) ) == NULL )
exit( EXIT_FAILURE );

strcpy(tmp[0], dirinfo->d_name);

}
else
{
size[1]=strlen(dirinfo->d_name) + 1;
if ( ( tmp[1] = malloc( size[1] ) ) == NULL )
exit( EXIT_FAILURE );
strcpy(tmp[1], dirinfo->d_name);
readFile(fd, directory, tmp, strlen(directory),
size);
sleep(3);
free(tmp[0]);
free(tmp[1]);
tmp[0] = NULL;
tmp[1] = NULL;
trouve=0;
}
}

}
}
closedir(dirh);
return num;
}




void readFile(int fd,char *directory, char *filename[], int sizedir,
int sizefile[])
{
FILE *fp;
char *line[2];
char *tmp;
int i;
int size[2];

if ( ( tmp = malloc( sizedir + sizefile[0] + 1) ) == NULL )
exit( EXIT_FAILURE );

strcpy(tmp, directory);
strcat(tmp, "/");
strcat(tmp, filename[0]);

fp = fopen(tmp, "r");
if(fp != NULL)
{
if ( ( line[0] = malloc( LINE_MAX) + 1) == NULL )
exit( EXIT_FAILURE );

fgets(line[0], LINE_MAX, fp);
fclose(fp);
free(tmp);

if ( ( line[1] = malloc( LINE_MAX) + 1 ) == NULL )
exit( EXIT_FAILURE );

if ( ( tmp = malloc( sizedir + sizefile[1] + 1 + 1) ) == NULL
)/*+1 for / and another +1 for \0 */
exit( EXIT_FAILURE );

strcpy(tmp, directory);
strcat(tmp, "/");
strcat(tmp, filename[1]);

fp = fopen(tmp, "r");
if(fp != NULL)
{
fgets(line[1], LINE_MAX, fp);
size[0]= strlen(line[0])+1;
size[1]= strlen(line[1])+1;

analyzeFilename(fd, filename, line, size);
fclose(fp);
}
else
fprintf(stderr, "%s - Not able to open the file: %s\n",
strerror(errno),filename[1] );
}
else
fprintf(stderr, "%s - Not able to open the file: %s\n",
strerror(errno),filename[0]);

free(tmp);
}


what i would like to do in my analyzeFilename function is: remove the
\0 to the led_line string (this string come from readFile and it's
tmp...)

why i want to do that:

i analyse string, i need to send p to a serial port...

right now if i print p i get somethink like:

......Z00.AB."b.1 1111..&b.12222..

but i want

......Z00.AA."b.11111.&b.12222.

i need to remove \0 from tmp... don't know if it's better to remove it
in the analyse function or directly in readFile function
 
D

David Resnick

collinm said:
David said:
dresnick wrote
size[0] = strlen(dirinfo->d_name) + 1;
if ( ( tmp[0] = malloc(size[0]) ) == NULL )
exit( EXIT_FAILURE );
memcpy(tmp[0], dirinfo->d_name, size[0]);
ok for the +1 in the maloc... but why memcpy?

You are otherwise leaking the malloced memory. Your original
code was this:
if ( ( tmp[0] = malloc(size[0]) ) == NULL )
exit( EXIT_FAILURE );
tmp[0] = dirinfo->d_name;

This is akin to the following simplified example:

char *a = malloc(10);
a = "foo";

The address of the memory returned
by malloc is no longer known to you, it has been leaked. That
is what you do above when assigning tmp[0] to dirinfo->d_name --
you make the memory returned by the malloc no longer reachable.

does

if ( ( tmp[0] = malloc(size[0]) ) == NULL )
exit( EXIT_FAILURE );
strcpy(tmp[0], dirinfo->d_name);

resolve this problem
Yes, as long as you do the +1. I just used memcpy because it is more
efficient. I in fact rarely use strcpy -- it is usually the case that
I know the length of the string I want copied, since I need to know
that
to be sure it fits in the target buffer...
Clearly you wanted tmp[0] to get a copy of dirinfo->d_name,
hence the memcpy. Perhaps you believed that assigning
tmp[0] = dirinfo->d_name copies the bytes from the d_name
into the space pointed at by tmp[0]? yes

in readFile function, when i print tmp, i get the correct value

Yes, because the pointer points to the name. But you hadn't made
a copy, so if the value in the original dirent goes away you have a
pointer to crud.
It does not, it just
changes tmp[0] to point at the location indicated by
dirinfo->d_name.

Your code would simpler if you implement a new function,
dupstr, that returns a malloc'd copy of a string (or dies
if it can't malloc), as you do that twice in your code.

ok something like

char * function_test()
{

if ( ( tmp = malloc( sizedir + sizefile[1] + 1) ) == NULL )
exit( EXIT_FAILURE );

return tmp;
}

I was thinking more like

char *dupstr(const char *source)
{
size_t len;
char *duplicate;

if (source == NULL) {
return NULL;
}
len = strlen(source) + 1;
duplicate = malloc(len);
if (duplicate == NULL) {
exit(EXIT_FAILURE); /* or deal however */
}
memcpy(duplicate, source, len);
return duplicate;
}

-David
 
D

Default User

collinm said:
ok for the +1 in the maloc... but why memcpy?

Please use the quoting version of the Google Reply for ALL your
postings. I know you know how, because you used it in your reply to
Martin.




Brian
 
E

Emmanuel Delahaye

collinm wrote on 08/04/05 :
trouve==0;

You meant
trouve=0;

Try this (Note : not ANSI code, but POSIX.1, probably...)

#include <dirent.h>

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int readFile (int fd, char *directory, char *tmp[2], int len, int
size[2])
{
printf ("fd = %d\n", fd);
printf ("directory = %s\n", directory);
printf ("tmp[0] = %s\n", tmp[0]);
printf ("tmp[1] = %s\n", tmp[1]);
printf ("len = %d\n", len);
printf ("size[0] = %d\n", size[0]);
printf ("size[1] = %d\n", size[1]);
return 0;
}

int searchFile (int fd, char *directory, char *ext)
{
int num = 0;
DIR *dirh = opendir (directory);

if (dirh != NULL)
{
int trouve = 0;
int size[2];

while (dirh != NULL)
{
struct dirent *dirinfo = readdir (dirh);

if (dirinfo != NULL)
{
char *file_ext = strrchr (dirinfo->d_name, '.');

if (file_ext != NULL)
{
file_ext++;
}
else
{
file_ext = dirinfo->d_name;
}

if (strcmp (file_ext, ext) == 0)
{
char *tmp[2];

trouve++;
if (trouve == 1)
{
size[0] = strlen (dirinfo->d_name);
tmp[0] = malloc (size[0]);
if (tmp[0] == NULL)
{
exit (EXIT_FAILURE);
}
strcpy (tmp[0], dirinfo->d_name);
}
else
{
size[1] = strlen (dirinfo->d_name);
if ((tmp[1] = malloc (size[1])) == NULL)
exit (EXIT_FAILURE);
strcpy (tmp[1], dirinfo->d_name);

readFile (fd, directory, tmp, strlen (directory),
size);
free (tmp[0]), tmp[0] = NULL;
free (tmp[1]), tmp[1] = NULL;
trouve = 0;
}
}
num++;
}
else
break;
}
closedir (dirh);
}
return num;
}

int main (void)
{
int ret = searchFile (-1, "..", "c");

printf ("ret = %d\n", ret);
return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
E

Emmanuel Delahaye

(supersedes <[email protected]>)

collinm wrote on 08/04/05 :
trouve==0;

You meant
trouve=0;

Try this (Note : not ANSI code, but POSIX.1, probably...)

#include <dirent.h>

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int readFile (int fd, char *directory, char *tmp[2], int len, int
size[2])
{
printf ("fd = %d\n", fd);
printf ("directory = %s\n", directory);
printf ("tmp[0] = %s\n", tmp[0]);
printf ("tmp[1] = %s\n", tmp[1]);
printf ("len = %d\n", len);
printf ("size[0] = %d\n", size[0]);
printf ("size[1] = %d\n", size[1]);
return 0;
}

int searchFile (int fd, char *directory, char *ext)
{
int num = 0;
DIR *dirh = opendir (directory);

if (dirh != NULL)
{
int trouve = 0;
int size[2];

while (dirh != NULL)
{
struct dirent *dirinfo = readdir (dirh);

if (dirinfo != NULL)
{
char *file_ext = strrchr (dirinfo->d_name, '.');

if (file_ext != NULL)
{
file_ext++;
}
else
{
file_ext = dirinfo->d_name;
}

if (strcmp (file_ext, ext) == 0)
{
char *tmp[2];

trouve++;
if (trouve == 1)
{
size[0] = strlen (dirinfo->d_name) + 1;
tmp[0] = malloc (size[0]);
if (tmp[0] == NULL)
{
exit (EXIT_FAILURE);
}
strcpy (tmp[0], dirinfo->d_name);
}
else
{
size[1] = strlen (dirinfo->d_name) + 1;
if ((tmp[1] = malloc (size[1])) == NULL)
exit (EXIT_FAILURE);
strcpy (tmp[1], dirinfo->d_name);

readFile (fd, directory, tmp, strlen (directory),
size);
free (tmp[0]), tmp[0] = NULL;
free (tmp[1]), tmp[1] = NULL;
trouve = 0;
}
}
num++;
}
else
break;
}
closedir (dirh);
}
return num;
}

int main (void)
{
int ret = searchFile (-1, "..", "c");

printf ("ret = %d\n", ret);
return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."
 
C

collinm

what i would like to do in my analyzeFilename function is: remove the
\0 to the led_line string (this string come from readFile and it's
tmp...)


why i want to do that:


i analyse string, i need to send p to a serial port...


right now if i print p i get somethink like:


.......Z00.AB."b.11111..&b.12222..

after 1111, you can see the \0

but i want

.......Z00.AA."b.11111.&b.12222 .
here after 11111 there is not \0


i need to remove \0 from tmp... don't know if it's better to remove it

in the analyse function or directly in readFile function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top