C programming fopen stdio.h


In C programming the <stdio.h> fopen function opens the file and associated a stream with it.In other words this function prepares the file for whatever activity you want to carry out with your file,so this function must be always called before you do anything with your file.The declaration of the function is shown below.

FILE *fopen(const char * restrict filename , const char * restrict mode);

Parameters:
filename -The name of the file you want to open.

restrict mode -This parameter will specify the type of activity you want to carry out with your file.For instance,if you want to read data from the file,in this case you will use the mode “r” to open the file,or if you want to append text to your existing file you can use the ‘mode’ “a” to open your file and so on.The various “restrict mode” and it’s function are given below.

ModeFunction
ropen text file for reading
wtruncate to zero length or create text file for writing
aappend; open or create text file for writing at end-of-file
rbopen binary file for reading
wbtruncate to zero length or create binary file for writing
abappend; open or create binary file for writing at end-of-file
r+open text file for update (reading and writing)
w+truncate to zero length or create text file for update
a+append; open or create text file for update, writing at end-of-file
r+b or rb+open binary file for update (reading and writing)
w+b or wb+truncate to zero length or create binary file for update
a+b or ab+append; open or create binary file for update, writing at end-of-file

Return type
FILE* -Returns pointer to the object controlling the stream.If ‘fopen’ fails null pointer is returned.

A code example is given below.

Code example

FILE *fp=fopen( “text.txt” , “w” ); /*open text.txt file for writting,if the file does not exit a new file with the name “text.txt” is created automatically */

if(fp!=NULL) //check if file opening succeeds
{
fputs( “New string ” , fp ); //Write the string “New string” to the text.txt file
}
else //if file opening fails
  printf(“File opening fails”);

fclose( fp ); //do not forget!!!

Link : C fclose stdio.h

Now run the program and go the directory where your main.cpp exist.In this directory you will find your file “text.txt” and open it you will see the string “New string”>.





Some points to note

The “r” mode

i) If the file ‘mode’ open with is “r” then ‘fopen’ fails if the file does not exist.

Code example

FILE *fp=fopen( “text.txt” , “r” );

if( fp!=NULL )
{
printf(“text.txt file exists”);
}
else
  printf(“text.txt does not exists”);

fclose( );

If ‘text.txt’ does not exist you will get the output as “text.txt does not exists”.


The append mode

ii) If the file is open with append mode:”a” or “a+” or “ab” or “a+b” or “ab+”,the subsequent writing to the file is always performed at the current end-of-file:at the end.This holds true even if you call ‘fseek’ to position the file at the beginning of the file.In short the append mode will always add the data to the end of the file.

Code example

FILE *fp=fopen( “text.txt” , “a” );

if( fp!=NULL )
{
fputs( “New string ” , fp );
}
else
  printf(“File opening fail”);

fseek( fp , 0 , SEEK_SET ); //position the file at the beginning

fputs( “123 New string ” , fp );

fclose( fp );

In running the program a new file “text.txt” is created with the text “New string 123 new string“.Note here even if we have position the file to the beginning the second string “123 new string” is added to the end-of-file not at the beginning of the file.


The mode with “+” character

iii) When a file is opened with update mode i.e. with the “+” as the second or third character in the ‘mode’ value,both input an output can be performed.However,output shall not be directly followed by input without any intervening call to ‘fllusf’ or call to a file positioning function:fseek,rewind,etc. And any input shall not be directly followed by output without any intervening call to a file positioning function, unless the input operation encounters end-of-file.

In the first program we will write text to a file and then try to read from the same file without calling any file positioning function>

Code example

FILE *fp=fopen( “New_text.txt” , “w+” ); //file open in “w+” mode

if( fp!=NULL )
{
/** Writing to file **
fputs( “My new text file ” , fp );
}
else
  printf(“File opening fail”);

int i=0;

/* Reading from file **/
while ( (i = fgetc(fp)) != EOF )
{
putchar( i );   //outputting the content of the file
}

fclose( fp );

Now if you run the program in Visual Studio the output looks something like this,

══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════…

I don’t know what this means-some gibberish values- and the if you open the file “New_text.txt” the the content look like this,

My new text fileÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ…

Again it doesn’t make sense.This happen because we have not called any file positioning function before reading from the file.The program below includes the file positioning function ‘rewind’ in this case the program works just fine.

Code example

FILE *fp=fopen( “New_text.txt” , “w+” ); //file open in “w+” mode

if( fp!=NULL )
{
/** Writing to file **
fputs( “My new text file ” , fp );
}
else
  printf(“File opening fail”);

int i=0;

rewind( fp );

/* Reading from file **/
while ( (i = fgetc(fp)) != EOF )
{
putchar( i );   //outputting the content of the file
}

fclose( fp );

Output in VS,

My new text file

If you open the file “New_text.txt” you will see the string “My new text file” written on it!.