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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define TAILLE 5000
typedef struct fichier
{
FILE* doc;
char* chemin;
} fichier;
void read(fichier*);
void write(fichier*);
void creat(fichier*);
void buf(){
int c = 0;
while ((c = getchar()) != '\n');
}
void wait(){
printf("\n Appuyez sur entrer pour retourner au menu...");
buf();
}
void strcle(char* str){
char* d = strchr(str, '\n');
if (d) *d = '\0';
else buf();
}
int main()
{
fichier monFichier;
monFichier.doc = NULL;
monFichier.chemin = NULL;
char choixSec[2];
int choix;
printf("\n Bienvenue sur zReader - lisez, cr\x82""ez et \x82""crivez dans vos fichiers !\n");
do{
printf("\n 1. Lire"
"\n 2. Ecrire"
"\n 3. Cr\x82""er"
"\n 4. Quitter \n"
"\n Votre choix ? ");
fgets(choixSec,2,stdin); buf();
choix = atoi(choixSec);
if(choix == 1) read(&monFichier);
else if(choix == 2) write(&monFichier);
else if(choix == 3) creat(&monFichier);
else if(choix == 4) break;
else printf("\n Mauvaise saisie !\n");
}while(1);
return EXIT_SUCCESS;
}
void read(fichier* monFichier)
{
monFichier->chemin = malloc(TAILLE * sizeof(char));
char c[TAILLE];
printf("\n Entrez le chemin du fichier : ");
fgets(monFichier->chemin, TAILLE, stdin);
strcle(monFichier->chemin);
printf("\n\n");
if(monFichier->doc = fopen(monFichier->chemin,"r"))
{
printf(" Contenu du fichier : \n\n");
while (fgets(c,TAILLE,monFichier->doc) != NULL)
printf(c);
fclose(monFichier->doc);
}
else
{
printf(" Le fichier n'existe pas ! \n");
}
free(monFichier->chemin);
wait();
}
void write(fichier* monFichier)
{
monFichier->chemin = malloc(TAILLE * sizeof(char));
char inputInFile[TAILLE*3];
printf("\n Entrez le chemin du fichier (si invalide, le fichier sera cr\x82""e) : ");
fgets(monFichier->chemin, TAILLE, stdin);
strcle(monFichier->chemin);
printf("\n\n");
monFichier->doc = fopen(monFichier->chemin,"w");
printf(" Saisie dans le fichier (pour fermer, tapez \"f.close\") : \n\n");
while(fgets(inputInFile,TAILLE*3,stdin))
{
if (strstr(inputInFile,"f.close"))
break;
fputs(inputInFile,monFichier->doc);
}
fclose(monFichier->doc);
free(monFichier->chemin);
printf("\n Contenu enregistr\x82"".");
wait();
}
void creat(fichier* monFichier)
{
char path[TAILLE];
char ext[30];
printf("\n\n Nom du fichier (extension non-incluse) : ");
fgets(path,TAILLE,stdin);
strcle(path);
printf("\n Extension du fichier : ");
fgets(ext,30,stdin);
strcle(ext);
strcat(path,".");
strcat(path,ext);
monFichier->doc = fopen(path,"w");
fclose(monFichier->doc);
printf("\n Fichier cr\x82""e avec succ\x8a""s \n\n");
} |
Partager