Opening a file pointer using fopen

Your program must open a file before it can access it. This is done using the fopen function, which returns the required file pointer. If the file cannot be opened for any reason then the value NULL will be returned. You will usually use fopen as follows


  if ((output_file = fopen("output_file", "w")) == NULL)
           fprintf(stderr, "Cannot open %s\n", "output_file");

fopen takes two arguments, both are strings, the first is the name of the file to be opened, the second is an access character, which is usually one of:

As usual, use the man command for further details by typing man fopen.


  Go to C file handling     Go to Index               Go to file pointers