No cache version.

Caching disabled. Default setting for this page:enabled (code LNG204)
If the display is too slow, you can disable the user mode to view the cached version.

fgetc/fputc: Lecture/écriture...

Lors des opérations d'entrée/sortie sur des fichiers, nous pouvons lire ou écrire caractère par caractère, ou ligne par ligne.
Pour les opérations caractère par caractère, nous emploierons les fonctions fgetc et fputc.

Syntaxe

#include <stdio.h>

/* Suite du code ... */

int fgetc(FILE *pointeur_entree);
int fputc(int caractere, FILE *pointeur_sortie);

/* Suite du code ... */

Fonction fgetc :

  • Lit le caractère courant depuis le fichier d'entrée spécifié.
  • La constante EOF est renvoyée si le pointeur atteint la fin du fichier.

Fonction fputc :

  • Ecrit un caractère à l'emplacement courant du pointeur de fichier, dans le fichier de sortie spécifié.
  • La constante EOF est renvoyée en cas d'erreur.

Exemple

Le programme « sauveconf » nous permet de copier le contenu du fichier config.sys dans un fichier nommé config.txt.

  1. #include <stdio.h>
  2.  
  3. void main (void)
  4. {
  5. FILE *input, *output;
  6. int lettre;
  7. if ((input=fopen("\\CONFIG.SYS","r"))==NULL)
  8. printf("Erreur ouverture de \\CONFIG.SYS\n");
  9. else if ((output=fopen("\\CONFIG.TXT","w"))==NULL)
  10. printf("Erreur ouverture de \\CONFIG.TXT\n");
  11. else
  12. {
  13. //lecture et ecriture de chaque caractere dans le fichier
  14. while (lettre=fgetc(input))!=EOF)
  15. fputc(lettre, output);
  16. fclose(input); //Fermeture du fichier d'entree
  17. fclose(output); //Fermeture du fichier de sortie
  18. }
  19. }

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 29/12/2002, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/c-fgetc-fputc.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.