Thread: problem to close file and get 1 character of...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    5

    problem to close file and get 1 character of...

    hi

    i got some error when i close file

    my searchFile function:
    Code:
    char *tmp[2];
    int size[2];
    ...
    
    trouve++;
    if( trouve == 1 ){
      sleep(3);
      printf("fic: %s\n", dirinfo->d_name);
                        
      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[0] = malloc( size[1]) ) == NULL ) 
        exit( EXIT_FAILURE ); 
      tmp[1] =  dirinfo->d_name;    
      readFile(fd, directory, tmp, strlen(directory), size);
      /*do a pause*/
      sleep(3);
      free(tmp[0]);
      free(tmp[1]);
      free(tmp);
      trouve==0;
    }
    ...
    my readFile function:
    Code:
    void readFile(int fd,char *directory, char *filename[], int sizedir, int sizefile[])
    {
        FILE *fp;
        char line[LINE_MAX][2];
        char *tmp; 
        int i;
        int size[2];
        //problème potentièle ? fin du tableau fini par \0 ?
        for(i=0; i<2; i++)
        {
            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)
            {
                fgets(line[0], LINE_MAX, fp);
                close(fp);
                
                free(tmp); 
                
                if ( ( tmp = malloc( sizedir +  sizefile[1] + 1) ) == NULL ) 
                    exit( EXIT_FAILURE ); 
                
                strcpy(tmp, directory);
                strcat(tmp, "/");
                strcat(tmp, filename[1]);
                
                /* On lie le fichier pour la deuxième ligne*/
                fp = fopen(tmp, "r");
                if(fp != NULL)
                {
                    fgets(line[1], LINE_MAX, fp);
                    size[0]= strlen(line[1]);
                    size[1]= strlen(line[2]);
                    analyzeFilename(fd, *filename, line, size);
                    close(fp);
                }
                else
                    fprintf(stderr, "%s - Not able to open the file\n", strerror(errno));
                
            }
            else
                fprintf(stderr, "%s - Not able to open the file\n", strerror(errno));
        }       
        free(tmp);        
    }
    in this function,
    Code:
    led_display.c: In function `readFile':
     warning: passing arg 1 of `close' makes integer from pointer without a cast
     warning: passing arg 1 of `close' makes integer from pointer without a cast
    i try also to get the first character of *filename[ 0 ];

    i do:
    Code:
    char *tmp;
    tmp=filename[ 0 ];       
    printf("1 char: %s\n", *tmp);
    i see: 1 char: (null)


    any idea?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Did you mean to use fclose instead of close?

    Code:
    char tmp = filename[ 0 ]; /* [edit]oops[/edit] */
    printf("1 char: %c\n", tmp);
    Last edited by Dave_Sinkula; 04-07-2005 at 11:57 AM. Reason: Oops. Bad code example -- didn't look closely enough.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    5
    Quote Originally Posted by Dave_Sinkula
    Did you mean to use fclose instead of close?

    Code:
    char tmp = filename[ 0 ];       
    printf("1 char: %c\n", tmp);
    it's ok now for the fclose... but now to get the 1 character?

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Since filename is an array of pointers to characters according to the function parameters:

    Code:
    char *tmp;
    tmp=filename[ 0 ];       
    printf("1 char: %c\n", tmp[0]);
    Or alternatively you could just make tmp a char by making the following changes:

    Code:
    char tmp;
    tmp=filename[0][0];
    printf("1 char: %c\n", tmp);
    Last edited by 0rion; 04-07-2005 at 11:42 AM.

Popular pages Recent additions subscribe to a feed