Ecriture de fichier

Résolu
crazyghandi Messages postés 323 Statut Membre -  
crazyghandi Messages postés 323 Statut Membre -
Bonjour,

je voudrais ecrire un fichier dans un sous repertoire en C

avec opendir je me place dans le repertoire cible,

pr tester, je demande a voir les fichiers avec readdir, il m'affiche bien les seuls fichierzs
dans le sous dossier ou on est

ensuite je veux creer un fichier avec open et mettre des infos dedans

la fonction marche mais le nouvo fichier est sauvegarde a la racine et non pas dans le repertoir
ou on se trouve

merci pour votre aide
Configuration: Windows XP
Opera 9.62

7 réponses

  1. mype Messages postés 2459 Date d'inscription   Statut Membre Dernière intervention   437
     
    tu le sauvegardes comment ton fichier ?
    poste le code pour voir
    0
  2. crazyghandi Messages postés 323 Statut Membre 19
     
    #include <unistd.h>
    #include <ctype.h>
    #include <fcntl.h>
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <dirent.h>
    #include <sys/stat.h>
    #include <time.h>
    #include <string.h>
    #include "typdef.h"
    #include "generate.h"

    //EXPORT INDEX
    void export_index (char *index_name, char *index_dir) {
    printf ("\n received : %s",index_dir);
    DIR *rep;
    char *root = "";
    char *file = malloc (sizeof (char) + 4);
    rep = opendir (index_dir);
    //default
    if (rep == NULL) {
    printf ("\n Index directory not found, setting to default.");
    closedir (rep);
    // strcpy (root,".");
    root = ".";
    rep = opendir (root);
    //index dir found
    }else{
    printf ("\n Index directory found");
    // strcpy (root,index_dir);
    }
    if (root == ".") {
    root = index_dir;
    printf ("\n root = '.'");
    }else{
    root = "index";
    }
    struct dirent *read;
    while (read = readdir (rep)) {
    printf("\n %s", read->d_name);
    }
    //file
    file = index_name;
    printf ("\n exporting to : %s",file);
    FILE *txtout;
    printf ("\n mark...");
    //open new index file
    //txt
    printf ("\n mark %s ",file);
    int l = strlen (file);
    printf ("\n length : %d",l);
    // file[l] = "\0";
    file = strcat (file,".txt");
    // file = root.".txt";
    printf ("\n mark, file = %s",file);
    //XML
    //strcat (root,".xml");
    txtout = fopen(file,"w");
    printf ("\n file creation : %d",txtout);
    printf ("\n ioen : %d",txtout);
    object *tmp1 = dlist;
    object *tmp2 = flist;
    //write subdirectories
    while (tmp1 != NULL) {
    if (tmp1->parent == root) {
    fprintf(txtout,"%d %s %d %s %s \n",
    tmp1->serial,tmp1->n,tmp1->size,tmp1->parent,tmp1->mdate);
    }
    tmp1 = tmp1->nxt;
    }
    //write files
    while (tmp2!= NULL) {
    if (tmp2->parent == root) {
    fprintf(txtout,"%d %s %d %s %s \n",
    tmp2->serial,tmp2->n,tmp2->size,tmp2->parent,tmp2->mdate);
    }
    tmp2 = tmp2->nxt;
    }
    //close new index file
    fclose(txtout);
    free (file);
    //end
    return;
    }
    0
  3. crazyghandi Messages postés 323 Statut Membre 19
     
    en fait je me suis perdu jremarque des erreurs pas normales je vais recommencer cette fonction

    je reviens si j'ai des soucis

    merci encore
    0
  4. mype Messages postés 2459 Date d'inscription   Statut Membre Dernière intervention   437
     
    oui forcement puisque quand tu cree ton fichier tu specifie pas le repertoire il faudrait que tu trouve un moyen de recupere le chemin jusqu'au sous repertoire et ensuite dans ton fopen tu fais
    txtout = fopen(/chemin/file,"w");
    0
  5. Vous n’avez pas trouvé la réponse que vous recherchez ?

    Posez votre question
  6. crazyghandi Messages postés 323 Statut Membre 19
     
    Ok voila la nouvelle fonction qui marche parfaitement excepte ce probleme de repertoire

    donc d'apres toi il faut ke jinclue le chemin dans le nom du fichier a ouvrir?

    ce qui est bizarre c'est que jouvre le repertoire pr me placer dedans et la fonction
    readdir me confirme ke je sui bien dan le sous repertoire alor pkoi kan jouvre un nouvo fichier il se replace
    a la racine?

    je vais tester en concatenant de foncon a avoir dir1\dir2.txt

    #include <unistd.h>
    #include <ctype.h>
    #include <fcntl.h>
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <dirent.h>
    #include <sys/stat.h>
    #include <time.h>
    #include <string.h>
    #include "typdef.h"
    #include "generate.h"

    //EXPORT INDEX
    void export_index (char *index_name, char *dest) {
    printf ("\n received :%s | %s",index_name,dest);
    DIR *rep;
    object *tmp1 = dlist;
    object *tmp2 = flist;
    char *root = malloc (sizeof (dest));
    char *temp = malloc (sizeof (index_name));
    char *file = malloc (sizeof (index_name) + 4);
    strcpy (root, dest);
    strcpy (file, index_name);
    strcpy (temp, index_name);
    printf ("\n mark : directory %s, file : %s", root, file);
    rep = opendir (root);
    printf ("\nopendir : %d",rep);
    //analyse index dir
    //default
    if (rep == NULL) {
    printf ("\n Index directory not found, setting to default.");
    closedir (rep);
    strcpy (root, ".");
    rep = opendir (root);
    //index dir found
    }else{
    printf ("\n Index directory found");
    }
    struct dirent *read;
    while (read = readdir (rep)) {
    printf("\n %s", read->d_name);
    }

    //analyse file
    //file name
    if (strcmp (file, ".") == 0) {
    printf ("\n copyin file : index");
    strcpy (file, "index");
    }
    //open new index file
    //txt
    strcat (file, ".txt");
    //XML
    //strcat (root,".xml");
    FILE *txtout;
    txtout = fopen(file,"w");
    printf ("\n file creation : %d",txtout);
    //write subdirectories
    printf ("\n mark : directory : %s, file : %sprinting...",root, file);
    printf ("\n test : %s, rott : %s",tmp2->n,root);
    tmp2 = flist;
    while (tmp1 != NULL) {
    if (!strcmp (tmp1->parent,temp)) {
    printf ("\n printing dir");
    fprintf(txtout,"%d %s %d %s %s \n",
    tmp1->serial,tmp1->n,tmp1->size,tmp1->parent,tmp1->mdate);
    }
    tmp1 = tmp1->nxt;
    }
    //write files
    tmp1 = dlist;
    while (tmp2!= NULL) {
    if (!strcmp (tmp2->parent,temp) || tmp1 != 0) {
    printf ("\n printing fil : %s",tmp2->n);
    fprintf(txtout,"%d %s %d %s %s \n",
    tmp2->serial,tmp2->n,tmp2->size,tmp2->parent,tmp2->mdate);
    }
    tmp2 = tmp2->nxt;
    }
    //close new index file
    fclose(txtout);
    free (file);
    free (root);
    //end
    return;
    }
    0
  7. mype Messages postés 2459 Date d'inscription   Statut Membre Dernière intervention   437
     
    oui mais c'est une histoire de flux avec ton opendir tu recupere un flux (rep) mais quand tu ecris ton fichier tu utilise un autre flux (textout)
    tu pourrais peut etre essayer d'ecrire avec le flux rep par sur que ça marche puisque rep est un flux repertoire et normalement on utiliise un FILE *
    essaye
    rep = fopen(file,"w");
    bien sur il faudra aussi remplacer dans tous tes fprintf qui suive le flux txtout par rep
    0
  8. crazyghandi Messages postés 323 Statut Membre 19
     
    Bon ca y'est ca marche j'ai fait avec des concatenations de chaines il mets bien ce qu'il faut ou il faut :
    //EXPORT INDEX
    void export_index (char *index_name, char *dest) {
    DIR *rep;
    object *tmp1 = dlist;
    object *tmp2 = flist;
    char *root = malloc (8*sizeof (dest));
    char *temp = malloc (8*sizeof (index_name));
    char *file = malloc (8*sizeof (index_name));
    strcpy (root, dest);
    strcpy (temp, index_name);
    int test = 0;
    rep = opendir (temp);
    if (rep == NULL) {
    strcpy (temp,"index");
    }
    rep = opendir (root);
    //analyse index dir
    //default
    if (rep == NULL) {
    printf ("\n Index directory not found, setting to default.");
    closedir (rep);
    strcpy (root, ".");
    rep = opendir (root);
    //index dir found
    }else{
    printf ("\n Index directory found");
    file = strcat (root,"/");
    }
    //analyse file
    //file path
    if (!strcmp (temp, ".")) {
    file = strcat (file, "index");
    }else{
    file = strcat (file,temp);
    }
    //open new index file
    //txt
    file = strcat (file, ".txt");
    //XML
    //strcat (root,".xml");
    FILE *txtout;
    txtout = fopen(file,"w");
    //write subdirectories
    while (tmp1 != NULL) {
    if (!strcmp (tmp1->parent,temp)) {
    fprintf(txtout,"%d %s %d %s %s \n",
    tmp1->serial,tmp1->n,tmp1->size,tmp1->parent,tmp1->mdate);
    }
    tmp1 = tmp1->nxt;
    }
    //write files
    tmp1 = dlist;
    tmp2 = flist;
    while (tmp2!= NULL) {
    int test = 0;
    while (tmp1 != NULL) {
    if (!strcmp (tmp1->n,tmp2->parent)) {
    test++;
    }
    tmp1 = tmp1->nxt;
    }
    tmp1 = dlist;
    if (!strcmp (tmp2->parent,temp) || test != 0) {
    fprintf(txtout,"%d %s %d %s %s \n",
    tmp2->serial,tmp2->n,tmp2->size,tmp2->parent,tmp2->mdate);
    }
    tmp2 = tmp2->nxt;
    }
    //close new index file
    fclose(txtout);
    printf ("\nSuccessfully exported to : %s", file);
    free (file);
    free (root);
    free (temp);
    //end
    return;
    }

    encore merci pour ton aide
    0