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
|
#include <stdio.h>
#include <assert.h>
static int fonction(char const *f_in, int numero)
{
int err = 0;
static char const f_out[] = "temp";
if (f_in != NULL)
{
FILE *input = fopen(f_in, "r");
if (input != NULL)
{
FILE *output= fopen(f_out, "w");
if (output!=NULL)
{
unsigned long line_count=0;
char ligne[1000];
while(fgets(ligne, sizeof ligne, input) != NULL)
{
line_count++;
fprintf(output, "%s", ligne);
}
fclose(output), output=NULL;
printf("%lu line%s copied\n", line_count, line_count>1?"s":"");
}
else
{
err=1;
perror(f_out);
}
fclose(input), input=NULL;
assert(output==NULL);
assert(input==NULL);
if (!err)
{
int debug = remove(f_in)
;
printf("remove: %d\n", debug);
debug = rename (f_out, f_in);
printf("rename: %d\n", debug);
}
}
else
{
err=1;
perror(f_in);
}
}
else
{
err=1;
}
return err;
}
int main (void)
{
fonction("test.txt", 10);
return 0;
} |
Partager