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
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define PREFIX "copie de "
#define NAME_SIZE 100
#define BUF_SIZE 200
char *askFileName();
FILE *fileOpen();
char fileName[NAME_SIZE];
char *askFileName()
{
/*char *str;
if(str = malloc(NAME_SIZE * sizeof(char)) == NULL){
printf("Erreur à la cration du buffer !\n");
getch(); fflush(stdin);
exit(1);
}*/
printf("\nNom du fichier texte a dupliquer: ");
scanf("%s", fileName); fflush(stdin);
return fileName;
}
FILE *fileOpen()
{
FILE *fp;
char c;
while((fp = fopen(askFileName(), "rt")) == NULL){
printf("\nErreur a l'ouverture du fichier %s: fichier inexistant ou corrompu.\n", fileName);
printf("\nAppuyez sur une touche pour continuer, Q pour quitter.\n");
c = getc(stdin); fflush(stdin);
if(c == 'q' || c == 'Q') exit(0);
}
return fp;
}
int main()
{
char fileName2[NAME_SIZE];
int i, offset = 0;
char buf[BUF_SIZE];
FILE *fp, *fp2;
fp = fileOpen();
fileName2[0] = '\0';
for(i = 0; i<strlen(fileName); i++) if(fileName[i] == '\\') offset = i;
sprintf(fileName2, "%s%s", PREFIX, &fileName[offset+0]);
if((fp2 = fopen(fileName2, "w")) == NULL){
printf("Erreur a l'ouverture du fichier: %s.\n", fileName2);
getch(); fflush(stdin);
exit(1);
}
i = 0;
fgets(buf, BUF_SIZE, fp); fprintf(fp2, "%s", buf);
while(feof(fp) == NULL)
{
i++;
fgets(buf, BUF_SIZE, fp);
fprintf(fp2, "%s", buf);
}
printf("\nfichier duplique avec succes");
fclose(fp);
fclose(fp2);
fflush(stdin); getch(); fflush(stdin);
} |
Partager