User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







c:stdio.h:fopen

fopen

#include <stdio.h> /* including standard library */
//#include <windows.h> /* uncomment this for Windows */
 
FILE * fopen (const char * restrict path, const char * restrict mode)

Opens file named restrict path and returns a stream, or NULL on failure. mode may be one of the following for files or one of those strings with b included (after the first character), for binary files.

Arguments

Open a file
Arguments for mode are:
“r” Open a file for reading
“w” Create / writing
“a” Append
“b” Open a binary file
“rb” Open binary file for reading
”+” Open file for read/write
“r+” text update (reading and writing)
“w+” text update, discarding previous content (if any)
“a+” text append, reading, and writing at end
example fopen(“test.txt”, “r+”);, fopen(“test.txt”, “w+”); fopen(“test.txt”, “rw”); …

C Sourcecode Example

/* 
 * fopen example code
 * http://code-reference.com/c/stdio.h/fopen 
 */
 
 
#include <stdio.h> /* including standard library */
//#include <windows.h> /* uncomment this for Windows */
 
 
int main( void )
{
  FILE *handle;
  handle = fopen("test.txt","r");
  if ( handle != 0 )
  {
    printf("fopen success:\n");
    fclose(handle);
  } else {
         printf("fopen failure\n");
 
         if (handle != NULL) {
             fclose(handle); 
          }
         return 1;
  }
  return 0;
}

fopen output example

  output: if no test.txt file there
  user@host:~$ ./fopen 
  fopen failure
  output: if test.txt exist
  user@host:~$ ./fopen 
  fopen success

on the occasion of the current invasion of Russia in Ukraine

Russian Stop this War
c/stdio.h/fopen.txt · Last modified: 2015/05/04 11:13 by 87.139.78.196

Impressum Datenschutz