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
| #include <stdio.h>
#include <stdlib.h>
#define MY_FILE "yourname.txt"
#define INPUT_SIZE 32
int main (void)
{
int ret = EXIT_SUCCESS;
FILE *fname = fopen (MY_FILE,"w");
if (fname != NULL)
{
char *input = malloc (INPUT_SIZE * sizeof *input);
if (input != NULL)
{
if (fgets (input, INPUT_SIZE, stdin) != NULL)
{
fputs(input, fname);
}
else if (ferror (stdin) != 0)
{
perror ("fgets");
ret = EXIT_FAILURE;
}
free (input), input = NULL;
}
else
{
perror ("malloc");
ret = EXIT_FAILURE;
}
fclose (fname), fname = NULL;
}
else
{
perror ("fopen");
ret = EXIT_FAILURE;
}
return ret;
} |
Partager