#include <stdio.h> #include <stdlib.h> #include <string.h> int main (void){ int tab[10] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }; FILE *fichier; int i; fichier = fopen ("tableau.txt", "wb"); if (!fichier) { return EXIT_FAILURE; } fwrite (tab, sizeof tab, sizeof *tab, fichier); fclose (fichier); memset (tab, 0, sizeof tab); fichier = fopen ("tableau.txt", "rb"); if (!fichier) { return EXIT_FAILURE; } fread (tab, sizeof tab, sizeof *tab, fichier); fclose (fichier); for (i = 0; i < 10; i++) { printf ("%d\n", tab[i]); } return EXIT_SUCCESS; }
#include <stdio.h> #include <string.h> void main(){ FILE *fPtr; int lg; char chaine[12]; fPtr = fopen("examen.txt", "r"); fgets(chaine, 12, fPtr); printf("%s\n", chaine); lg = strlen(chaine); printf("%d\n", lg); fclose(fPtr); }
Écrire un programme qui demande à l'utilisateur de saisir le nom d'un fichier et qui copie le contenu du fichier dans nouveau fichier copie.txt. Si le nom saisi ne correspond à aucun fichier, le programme doit afficher l'indiquer Le fichier XXX n'existe pas.
copie.txt
Le fichier XXX n'existe pas
#include <stdio.h> // Exercice 5 void main(){ char ch, nom[50]; FILE *file1, *file2; printf("Veuillez entrer le nom du fichier: "); scanf("%s", nom); // ouverture du fichier file1 = fopen(nom, "r"); if (file1) { // le fichier a été bien ouvert file2 = fopen("copie.txt", "w"); // Lire le contenu du fichier while((ch = getc(file1)) != EOF){ putc(ch, file2); } printf("Fichier copier\n"); fclose(file2); } else{ // l'ouverture a échoué printf("Le fichier %s n'existe pas\n", nom); } fclose(file1); }