Ecriture de fichier
Résolu/Fermé
crazyghandi
Messages postés
312
Date d'inscription
vendredi 9 novembre 2007
Statut
Membre
Dernière intervention
4 octobre 2011
-
1 déc. 2008 à 16:37
crazyghandi Messages postés 312 Date d'inscription vendredi 9 novembre 2007 Statut Membre Dernière intervention 4 octobre 2011 - 2 déc. 2008 à 01:14
crazyghandi Messages postés 312 Date d'inscription vendredi 9 novembre 2007 Statut Membre Dernière intervention 4 octobre 2011 - 2 déc. 2008 à 01:14
A voir également:
- Ecriture de fichier
- Fichier rar - Guide
- Fichier host - Guide
- Fichier iso - Guide
- Écriture facebook - Guide
- Comment réduire la taille d'un fichier - Guide
7 réponses
mype
Messages postés
2435
Date d'inscription
jeudi 1 novembre 2007
Statut
Membre
Dernière intervention
16 août 2010
436
1 déc. 2008 à 16:49
1 déc. 2008 à 16:49
tu le sauvegardes comment ton fichier ?
poste le code pour voir
poste le code pour voir
crazyghandi
Messages postés
312
Date d'inscription
vendredi 9 novembre 2007
Statut
Membre
Dernière intervention
4 octobre 2011
19
1 déc. 2008 à 16:59
1 déc. 2008 à 16:59
#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;
}
#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;
}
crazyghandi
Messages postés
312
Date d'inscription
vendredi 9 novembre 2007
Statut
Membre
Dernière intervention
4 octobre 2011
19
1 déc. 2008 à 17:03
1 déc. 2008 à 17:03
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
je reviens si j'ai des soucis
merci encore
mype
Messages postés
2435
Date d'inscription
jeudi 1 novembre 2007
Statut
Membre
Dernière intervention
16 août 2010
436
1 déc. 2008 à 17:15
1 déc. 2008 à 17:15
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");
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
crazyghandi
Messages postés
312
Date d'inscription
vendredi 9 novembre 2007
Statut
Membre
Dernière intervention
4 octobre 2011
19
1 déc. 2008 à 18:13
1 déc. 2008 à 18:13
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;
}
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;
}
mype
Messages postés
2435
Date d'inscription
jeudi 1 novembre 2007
Statut
Membre
Dernière intervention
16 août 2010
436
1 déc. 2008 à 18:22
1 déc. 2008 à 18:22
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
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
crazyghandi
Messages postés
312
Date d'inscription
vendredi 9 novembre 2007
Statut
Membre
Dernière intervention
4 octobre 2011
19
2 déc. 2008 à 01:14
2 déc. 2008 à 01:14
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
//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