Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Zortech C++ Language Reference - fopen http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
   fopen

   Usage
   #include <stdio.h>
   FILE *fopen(char *name,char *mode);

   Description
   fopen  opens a file. name gives the filename to be opened. mode  is  a
   character  string  indicating how the file is to be  opened.  Possible
   values for mode are:

   "r"  for reading
   "w"  for writing (truncates any existing file with the same name)
   "a"  for  appending (if file exists then open for writing at  end  of
        file, else create the file)
   "r+" for reading and writing
   "w+" for reading and writing (if file exists then truncate  it,  else
        create it)
   "a+" for reading and writing (if file exists then position at end  of
        file, else create it)

   In  addition, a b may be appended to the mode string to indicate  that
   the file is to be opened in binary mode (the default is text mode). If
   a  file is opened for reading and writing, only reads or  only  writes
   can  be  done at any one time. To switch from reading to  writing,  or
   vice  versa, an fseek must be performed on the stream,  unless  during
   input an EOF was read.

   Example
   #include <stdio.h>
   main()
   {
   FILE *fp;
        fp = fopen("file.dat","r");
        printf("Open text file\n");

        if(fp)
             fclose(fp);
        fp = fopen("CON","wb");

        printf("Sound bell\n");
        fputc(0x07,fp);

        if(fp)
             fclose(fp);
        fp = fopen("CON","rb");
        printf("Enter string\n");

        if(fp && fgetc(fp) == 3)
             printf("\n Ctrl-C entered\n");

        if(fp)
             fclose(fp);
        fp = fopen("PRN","w");
        printf("Write to printer\n");

        if(fp)
             fclose(fp);
        fp = fopen("file.dat","a+");
        printf("ASCII update file\n");

        if(fp)
             fclose(fp);
   }

   Return Value
   fopen  returns  a FILE pointer to an open file. A NULL  pointer  value
   indicates an error.


See Also: fclose freopen open

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson