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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 1000 /* Taille de chaîne maximale */
#define BASE 10 /* Base décimale */
#define FAIL 0
#define SUCCESS 1 /* Constantes pour les retours de fonction */
typedef struct File
{
FILE* ptr;
char* path;
} File;
int menu(long*);
void readFile(File*);
void createFile(File*);
void writeFile(File*);
void clean(void);
int read_string(char*, const int);
long read_long(long**);
void error(void);
int main(void)
{
long ret, retMenu;
File my_file;
my_file.ptr = NULL;
my_file.path = NULL;
printf("\n\n zReader - Manipulez vos fichiers");
do
{
if((ret = menu(&retMenu)) == FAIL)
error();
if(retMenu == 1)
readFile(&my_file);
else if(retMenu == 2)
createFile(&my_file);
else if(retMenu == 3)
writeFile(&my_file);
else if(retMenu == 4)
break;
else
printf("\n Mauvaise saisie, recommencez !\n\n");
} while(1);
return EXIT_SUCCESS;
}
int menu(long* ret)
{
printf( "\n\n\n MENU\n"
" 1 - Lire un fichier\n"
" 2 - Créer un fichier\n"
" 3 - Ecrire dans un fichier\n"
" 4 - Quitter le programme\n\n"
" Votre choix ? ");
if((read_long(&ret)) == FAIL)
return FAIL;
else
return SUCCESS;
}
void readFile(File* my_file)
{
int retPath;
char str[MAX_LEN] = "";
my_file->path = malloc(MAX_LEN * sizeof(my_file->path));
while(1)
{
printf("\n\n Entrez le chemin du fichier que vous souhaitez lire : ");
if((retPath = read_string(my_file->path, MAX_LEN)) == FAIL)
error();
if((my_file->ptr = fopen(my_file->path, "r")) != NULL)
break;
else
printf("\n Mauvaise saisie : le fichier n'existe pas, recommencez !\n");
}
printf("\n Contenu du fichier : \n\n");
if(fgetc(my_file->ptr) != EOF)
{
fseek(my_file->ptr, 0, SEEK_SET);
while(fgets(str, MAX_LEN, my_file->ptr) != NULL)
printf("%s", str);
}
else printf(" Désolé, le fichier est vide !");
fclose(my_file->ptr);
free(my_file->path);
}
void createFile(File* my_file)
{
int retPath;
my_file->path = malloc(MAX_LEN * sizeof(my_file->path));
while(1)
{
printf("\n\n Entrez le chemin du fichier que vous souhaitez créer : ");
if((retPath = read_string(my_file->path, MAX_LEN)) == FAIL)
error();
if((my_file->ptr = fopen(my_file->path, "w")) != NULL)
break;
else
printf("\n Mauvaise saisie : le chemin du fichier n'existe pas, recommencez !\n");
}
printf("\n Le fichier a bien été créé !");
fclose(my_file->ptr);
free(my_file->path);
}
void writeFile(File* my_file)
{
int retPath, c;
char str[MAX_LEN * 5] = "";
my_file->path = malloc(MAX_LEN * sizeof(my_file->path));
while(1)
{
printf("\n\n Entrez le chemin du fichier que vous souhaitez lire : ");
if((retPath = read_string(my_file->path, MAX_LEN)) == FAIL)
error();
if((my_file->ptr = fopen(my_file->path, "w")) != NULL)
break;
else
printf("\n Mauvaise saisie : le fichier n'existe pas, recommencez !\n");
}
printf("\n Saisissez le texte à mettre dans le fichier (pour fermer, tapez \"close_file\") : \n\n");
while(c = read_string(str, sizeof(str)))
{
if(strstr(str, "close_file"))
break;
fputs(str, my_file->ptr);
}
if(c == FAIL)
error();
fclose(my_file->ptr);
free(my_file->path);
}
void clean(void)
{
int c = 0;
while ((c = getchar()) != '\n' && c != EOF);
}
int read_string(char *str, const int length)
{
char *ptr = NULL;
if(fgets(str, length, stdin) != NULL)
{
ptr = strchr(str, '\n');
if(ptr != NULL)
*ptr = '\0';
else
clean();
return SUCCESS;
}
else
{
clean();
return FAIL;
}
}
long read_long(long** ret)
{
char str[MAX_LEN] = {0};
if(read_string(str, 100))
{
**ret = strtol(str, NULL, BASE);
return SUCCESS;
}
else
return FAIL;
}
void error(void)
{
fprintf(stderr, "Erreur fatale lors de la saisie");
exit(EXIT_FAILURE);
} |
Partager