Refer to the recent articles on Understanding File Handling Functions in C & Understanding Advance File Handling Functions in C
C communicates with files using a new datatype called a file pointer. This type is defined within stdio.h, and written as FILE *. A file pointer called output_file is declared in a statement like
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
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:
"r" => Open a file for reading
"w" => Create a file for writing
"a" => Open a file for appending
The fclose command can be used to disconnect a file pointer from a file. This is usually done so that the pointer can be used to access a different file. Systems have a limit on the number of files which can be open simultaneously, so it is a good idea to close a file when you have finished using it.
This would be done using a statement like
If files are still open when a program exits, the system will close them for you. However it is usually better to close the files properly.
UNIX systems provide three file descriptors which are automatically open to all C programs. These are
stdin => The standard input.The keyboard or a redirected input file.
stdout => The standard output.The screen or a redirected output file.
stderr => The standard error.This is the screen, even when output is redirected.
This is a conventional place to put any error messages.
Since these files are already open, there is no need to use fopen on them.
C communicates with files using a new datatype called a file pointer. This type is defined within stdio.h, and written as FILE *. A file pointer called output_file is declared in a statement like
Code: C
FILE *output_file;
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
Code: C
if ((output_file = fopen("output_file", "w")) == NULL)
fprintf(stderr, "Cannot open %s\n", "output_file");
"r" => Open a file for reading
"w" => Create a file for writing
"a" => Open a file for appending
Closing a file using fclose
The fclose command can be used to disconnect a file pointer from a file. This is usually done so that the pointer can be used to access a different file. Systems have a limit on the number of files which can be open simultaneously, so it is a good idea to close a file when you have finished using it.
This would be done using a statement like
Code: C
fclose(output_file);
Standard file pointers in UNIX
UNIX systems provide three file descriptors which are automatically open to all C programs. These are
stdin => The standard input.The keyboard or a redirected input file.
stdout => The standard output.The screen or a redirected output file.
stderr => The standard error.This is the screen, even when output is redirected.
This is a conventional place to put any error messages.
Since these files are already open, there is no need to use fopen on them.
shabbir, vikasbhandare2000
likes this
