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.

Ouverture d'un fichier (fopen)

syntaxe fopen

#include <stdio.h>

/* Suite du code ... */

FILE *fopen(const char *nomfichier , const char *mode);

/* Suite du code ... */

Arguments

  • nomfichier : nom (ou chemin) externe du fichier à ouvrir.
    Par exemple: « C:\data\fichier1.txt ».
    Avant de lire ou d'écrire dans un fichier il faut "ouvrir le fichier". Cette opération permet d'associer (si vous en avez le droit) un flux au fichier désigné par son nom physique nom.
  • mode : mode d'ouverture.
    • r: Ouverture en lecture seule sur un fichier existant
    • w: Ouverture en écriture seule.
      Si le fichier existe il est détruit.
    • a: Ouverture pour écriture à la fin du fichier.
      Création du fichier s'il n'existe pas
    • r+: Ouverture en lecture ou écriture sur un fichier existant.
    • w+: Ouverture en lecture ou écriture.
      Si le fichier existe il est détruit.
      Création du fichier s'il n'existe pas.
    • a+: Ouverture en lecture ou écriture à la fin du fichier.
      Création du fichier s'il n'existe pas.

Remarque

Contrairement à MSDOS, il n'existe pas sous UNIX de différence entre un fichier binaire et un fichier texte. Ainsi, il existe sous MSDOS des modes d'ouvertures rb, wb, ab, r+b, w+b, a+b permettant d'ouvrir des fichiers en mode binaire et des modes d'ouvertures rt, wt, at, r+t, w+t, a+t permettant d'ouvrir des fichiers en mode texte.

Valeur retournée

Si elle aboutit, cette fonction retourne un pointeur sur le flux qui vient d'être ouvert; sinon, elle retourne NULL.

Contents Haut

Exemple

  1. /* test de l'ouverture en création */
  2.  
  3. #include <stdio.h>
  4.  
  5. void main (void)
  6. {
  7. FILE *flux;
  8. if ( (flux = fopen("toto.txt","w")) == NULL)
  9. {
  10. perror("fopen"); /* affichage d'un message d'erreur */
  11. exit(1);
  12. }
  13. return 0;
  14. }

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 19/03/2002, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/c-fopen.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.