codescracker


c

C fopen()



« Previous Function Next Function »


fopen()

fopen() function is used to open a file whose name is pointed to by fname and returns the stream that is associated with it. The type of operations that can be allowed on the file are defined by value of mode

Following table lists the legal value for mode that can be allowed on the file :

C Programming Table:
Mode Meaning
"r" Open a text file for reading
"w" Create a text file for writing
"a" Append content to text file
"rb" Open a binary file for reading
"wb" Create a binary file for writing
"ab" Append content to a binary file
"r+" Open text file for read and write both
"w+" Create a text file for read and write both
"a+" Open a text file for read and write(append) both
"rb+" or "r+b" Open a binary file for read and write both
"wb+" or "w+b" Create a binary file for read and write both
"ab+" or "a+b" Open a binary file for read and write(append) both

fopen() Syntax

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

A FILE pointer is returned if fopen() function is successful in opening the specified file, otherwise it will returns a null pointer

fopen() Example

Following c code shows the correct method to open a file:

FILE *fp;
fp=fopen("demo.txt", "w");
if(fp==NULL)
{
	printf("Error in opening the file..!!\n");
	printf("Press any key to exit..\n");
	getch();
	exit(1);
}

« Previous Function Next Function »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test