IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Linux Discussion :

ouverture ecriture et fermeture de fichier avec open(), write(), close()


Sujet :

Linux

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 6
    Points : 3
    Points
    3
    Par défaut
    Salut a tous,
    je dois ecrire un petit programme qui parcours recursivement un repertoire et ecrit dans un fichier appele data le chemin des fichiers ayant plusieurs lien et dans un autre fichier appele data2 les numeros d'inodes des fichiers rencontres. Si le fichier rencontre est un dossier, alors je retourne la fonction.

    Il semblerait que je sois dans l'impossibilite d'ecrire dans mon fichier inodes et je n'arrive pas a comprendre pourquoi....

    Quelqu'un aurait il des suggestion svp

    Merci d'avance

    voici mon code.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
     
     
    #include <stdio.h>
    #include <stdlib.h>     
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>                      
    #include <dirent.h>
     
    int checkAllFilesInDir(char * directory){
     
            DIR * dir;
            struct dirent * file;
     
            if ((dir = opendir(directory))==NULL){
                    return EXIT_FAILURE;
            }
     
            while ((file = readdir(dir))!=NULL){
                    if (strcmp(file->d_name,".")!=0 && strcmp(file->d_name,"..")!= 0){
                            struct stat file_info;
                            char file_path[0x100];
                            strncpy(file_path, directory, sizeof(file_path));
                            strncat(file_path, "/", sizeof(file_path));
                            strncat(file_path, file->d_name, sizeof(file_path));
     
                            if (lstat(file_path, &file_info)!=-1){
                                    if (S_ISDIR(file_info.st_mode)){
                                            closedir(dir);
                                            checkAllFilesInDir(file_path);
                                    }
                                    if (file_info.st_nlink > 1){
                                            int fh;
                                            if ((fh = open("./data", O_CREAT|O_APPEND|O_RDWR,0777))==-1){
                                                    perror("Unable to open data");
                                                    return EXIT_FAILURE;
                                            }
                                            if ((fh = write(fh, file_path, strlen(file_path)))==-1){
                                                    perror("Unable to write to data");
                                                    return EXIT_FAILURE;
                                            }
                                            if (close(fh)==-1){
                                                    perror("Close error");
                                                    return EXIT_FAILURE;
                                            }
                                    }
     
                                    int fh2;
                                    ino_t inode;
     
                                    if ((fh2 = open("data2", O_CREAT|O_APPEND|O_RDWR,0777))==-1){
                                                    perror("Unable to open inos");
                                                    return EXIT_FAILURE;
                                    }
                                    inode = file_info.st_ino;
                                    if (fh2 = write(fh2, inode, sizeof(ino_t))==-1){
                                            perror("Unable to write to inos");
                                            return EXIT_FAILURE;
                                    }
     
                                    if (close(fh2)==-1){
                                            perror("Close error");
                                            return EXIT_FAILURE;
                                    }
                           }
                    }
            }
            closedir(dir);
            return EXIT_SUCCESS;
    }
    voici le header
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    #define MULTIPLE_LINKED_FILE_CREATOR
    #ifndef MULTIPLE_LINKED_FILE_CREATOR
     
    int checkAllFilesInDir(char * directory);
     
    #endif
    voici le main
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    #include "MultipleLinkedFileCreator.h"
     
    int main( int argc, char * argv[]){
            char directory[0x100];
            char file_name[0x100];
     
            checkAllFilesInDir("/Users");// un repertoire quelconque
            return 0;
    }


    Bon ok j'ai plus ou moins reussi a cibler mon probleme
    il se situe au niveau de mon ecriture de fichier
    il semblerait que le numero de fichier soit incorrect.
    je n'y comprends rien

  2. #2
    Expert éminent sénior
    Avatar de Sve@r
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2006
    Messages
    12 690
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Février 2006
    Messages : 12 690
    Points : 30 984
    Points
    30 984
    Billets dans le blog
    1
    Par défaut
    Citation Envoyé par NomadSoul123 Voir le message
    Salut a tous,
    je dois ecrire un petit programme qui parcours recursivement un repertoire et ecrit dans un fichier appele data le chemin des fichiers ayant plusieurs lien et dans un autre fichier appele data2 les numeros d'inodes des fichiers rencontres. Si le fichier rencontre est un dossier, alors je retourne la fonction.

    Il semblerait que je sois dans l'impossibilite d'ecrire dans mon fichier inodes et je n'arrive pas a comprendre pourquoi....

    Quelqu'un aurait il des suggestion svp

    Merci d'avance

    voici mon code.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
     
     
    #include <stdio.h>
    #include <stdlib.h>     
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>                      
    #include <dirent.h>
     
    int checkAllFilesInDir(char * directory){
     
            DIR * dir;
            struct dirent * file;
     
            if ((dir = opendir(directory))==NULL){
                    return EXIT_FAILURE;
            }
     
            while ((file = readdir(dir))!=NULL){
                    if (strcmp(file->d_name,".")!=0 && strcmp(file->d_name,"..")!= 0){
                            struct stat file_info;
                            char file_path[0x100];
                            strncpy(file_path, directory, sizeof(file_path));
                            strncat(file_path, "/", sizeof(file_path));
                            strncat(file_path, file->d_name, sizeof(file_path));
     
                            if (lstat(file_path, &file_info)!=-1){
                                    if (S_ISDIR(file_info.st_mode)){
                                            closedir(dir);
                                            checkAllFilesInDir(file_path);
                                    }
                                    if (file_info.st_nlink > 1){
                                            int fh;
                                            if ((fh = open("./data", O_CREAT|O_APPEND|O_RDWR,0777))==-1){
                                                    perror("Unable to open data");
                                                    return EXIT_FAILURE;
                                            }
                                            if ((fh = write(fh, file_path, strlen(file_path)))==-1){
                                                    perror("Unable to write to data");
                                                    return EXIT_FAILURE;
                                            }
                                            if (close(fh)==-1){
                                                    perror("Close error");
                                                    return EXIT_FAILURE;
                                            }
                                    }
     
                                    int fh2;
                                    ino_t inode;
     
                                    if ((fh2 = open("data2", O_CREAT|O_APPEND|O_RDWR,0777))==-1){
                                                    perror("Unable to open inos");
                                                    return EXIT_FAILURE;
                                    }
                                    inode = file_info.st_ino;
                                    if (fh2 = write(fh2, inode, sizeof(ino_t))==-1){
                                            perror("Unable to write to inos");
                                            return EXIT_FAILURE;
                                    }
     
                                    if (close(fh2)==-1){
                                            perror("Close error");
                                            return EXIT_FAILURE;
                                    }
                           }
                    }
            }
            closedir(dir);
            return EXIT_SUCCESS;
    }
    Bon ok j'ai plus ou moins reussi a cibler mon probleme
    il se situe au niveau de mon ecriture de fichier
    il semblerait que le numero de fichier soit incorrect.
    je n'y comprends rien
    Tu écrits "fh=open(...)" puis "fh=write(...)". T'as pas l'impression de perdre quelque part un truc plutôt utile ???

    PS: Pourquoi ton message est-il indiqué avoir été modifié par Emmanuel Delahaye ???
    Mon Tutoriel sur la programmation «Python»
    Mon Tutoriel sur la programmation «Shell»
    Sinon il y en a pleins d'autres. N'oubliez pas non plus les différentes faq disponibles sur ce site
    Et on poste ses codes entre balises [code] et [/code]

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 6
    Points : 3
    Points
    3
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <dirent.h>
     
    int checkAllFilesInDir(char * directory){
     
            DIR * dir;
            FILE * descriptor;
            struct dirent * file;
     
            if ((dir = opendir(directory))==NULL){
                    return EXIT_FAILURE;
            }
     
            while ((file = readdir(dir))!=NULL){
                    if (strcmp(file->d_name,".")!=0 && strcmp(file->d_name,"..")!= 0){
                            struct stat file_info;
                            char file_path[0x100];
                            strncpy(file_path, directory, sizeof(file_path));
                            strncat(file_path, "/", sizeof(file_path));
                            strncat(file_path, file->d_name, sizeof(file_path));
     
                            if (lstat(file_path, &file_info)!=-1){
                                    if (S_ISDIR(file_info.st_mode)){
                                            checkAllFilesInDir(file_path);
                                    }
                                    if (S_ISREG(file_info.st_mode)){
                                            if (file_info.st_nlink > 1){
                                                    int fh;
                                                    if ((fh = open("data.txt", O_CREAT|O_APPEND&O_RDWR,0777))==-1){
                                                            perror("Unable to open data");
                                                            return EXIT_FAILURE;
                                                    }
                                                    if (write(fh, file_path, strlen(file_path))==-1){
                                                            perror("Unable to write to data");
                                                            return EXIT_FAILURE;
                                                    }
                                                    if (close(fh)==-1){
                                                            perror("Close error");
                                                            return EXIT_FAILURE;
                                                    }
                                            }
                                    }
                            }
                    }
            }
            closedir(dir);
            return EXIT_SUCCESS; 
    }
    Unable to write to data: Bad file number

  4. #4
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 6
    Points : 3
    Points
    3
    Par défaut
    Avec le write en commentaire tout se passe bien

    A l'aide !!!!


  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 6
    Points : 3
    Points
    3
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <dirent.h>
     
    int checkAllFilesInDir(char * directory, int fh){
     
            DIR * dir;
            FILE * descriptor;
            struct dirent * file;
     
            if ((dir = opendir(directory))==NULL){
                    return EXIT_FAILURE;
            }
            if ((fh = open("data.txt", O_CREAT|O_APPEND&O_RDWR,0777))==-1){
                    perror("Unable to open data");
                    return EXIT_FAILURE;
            }
     
            while ((file = readdir(dir))!=NULL){
                    if (strcmp(file->d_name,".")!=0 && strcmp(file->d_name,"..")!= 0){
                            struct stat file_info;   
                            char file_path[0x100];
                            strncpy(file_path, directory, sizeof(file_path));
                            strncat(file_path, "/", sizeof(file_path));
                            strncat(file_path, file->d_name, sizeof(file_path));
     
                            if (lstat(file_path, &file_info)!=-1){
                                    if (S_ISDIR(file_info.st_mode)){
                                            close(fh);
                                            int fh2;
                                            fh2 = dup(fh);
                                            checkAllFilesInDir(file_path, fh2);
                                    }       
                                    if (S_ISREG(file_info.st_mode)){
                                            if (file_info.st_nlink > 1){
                                                    if (write(fh, file_path, strlen(file_path))==-1){
                                                            perror("Unable to write to data");
                                                            return EXIT_FAILURE;
                                                            return EXIT_FAILURE;
                                                    }
                                                    if (close(fh)==-1){
                                                            perror("Close error");
                                                            return EXIT_FAILURE;
                                                    }
                                            }
                                    }
                            }
                    } 
            }
            closedir(dir);
            return EXIT_SUCCESS;            
    }
    bon il me manque plus que le fork je pense

  6. #6
    Expert éminent sénior
    Avatar de Sve@r
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2006
    Messages
    12 690
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Février 2006
    Messages : 12 690
    Points : 30 984
    Points
    30 984
    Billets dans le blog
    1
    Par défaut
    Citation Envoyé par NomadSoul123 Voir le message
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <dirent.h>
     
    int checkAllFilesInDir(char * directory, int fh){
     
            DIR * dir;
            FILE * descriptor;
            struct dirent * file;
     
            if ((dir = opendir(directory))==NULL){
                    return EXIT_FAILURE;
            }
            if ((fh = open("data.txt", O_CREAT|O_APPEND&O_RDWR,0777))==-1){
                    perror("Unable to open data");
                    return EXIT_FAILURE;
            }
     
            while ((file = readdir(dir))!=NULL){
                    if (strcmp(file->d_name,".")!=0 && strcmp(file->d_name,"..")!= 0){
                            struct stat file_info;   
                            char file_path[0x100];
                            strncpy(file_path, directory, sizeof(file_path));
                            strncat(file_path, "/", sizeof(file_path));
                            strncat(file_path, file->d_name, sizeof(file_path));
     
                            if (lstat(file_path, &file_info)!=-1){
                                    if (S_ISDIR(file_info.st_mode)){
                                            close(fh);
                                            int fh2;
                                            fh2 = dup(fh);
                                            checkAllFilesInDir(file_path, fh2);
                                    }       
                                    if (S_ISREG(file_info.st_mode)){
                                            if (file_info.st_nlink > 1){
                                                    if (write(fh, file_path, strlen(file_path))==-1){
                                                            perror("Unable to write to data");
                                                            return EXIT_FAILURE;
                                                            return EXIT_FAILURE;
                                                    }
                                                    if (close(fh)==-1){
                                                            perror("Close error");
                                                            return EXIT_FAILURE;
                                                    }
                                            }
                                    }
                            }
                    } 
            }
            closedir(dir);
            return EXIT_SUCCESS;            
    }
    O_APPEND & O_RDWR ?
    T'es certain de savoir comment fonctionne le système des flags de positionnement ????

    En outre j'imagine mal 1) pourquoi tu fermes "fh" dans le cas "S_IFDIR" et 2) comment se comporte le système quand tu fais un "dup()" d'un fichier fermé ?

    Euh... tu connais cet outil absolument génial et hyper utile qui se nomme "commentaire" ? C'est génial. Ca te permet d'indiquer dans ton code ce que tu es en train de faire et pourquoi tu le fais. Comme ça ceux qui relisent ton code arrivent mieux à te comprendre...

    Citation Envoyé par NomadSoul123 Voir le message
    bon il me manque plus que le fork je pense
    Je pense aussi...
    Mon Tutoriel sur la programmation «Python»
    Mon Tutoriel sur la programmation «Shell»
    Sinon il y en a pleins d'autres. N'oubliez pas non plus les différentes faq disponibles sur ce site
    Et on poste ses codes entre balises [code] et [/code]

Discussions similaires

  1. Réponses: 1
    Dernier message: 22/09/2014, 18h57
  2. créer un fichier avec open et read
    Par l1informatique dans le forum Général Python
    Réponses: 4
    Dernier message: 20/08/2014, 12h01
  3. [XL-2007] Plantage excel à la fermeture du fichier avec macro si autre fichier excel ouvert
    Par Systémicien dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 31/03/2011, 09h00
  4. Réponses: 0
    Dernier message: 28/05/2009, 18h53
  5. Problème d'E.S. sur fichier avec open
    Par Bubonik software dans le forum C
    Réponses: 6
    Dernier message: 04/02/2004, 19h42

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo