C Files

C files are nothing but the files in the computer which are read, written, and created using C compilers. Opening a file in C: The type ‘FILE’ is used for a file variable and is defined in the ‘stdio.h’ file. It is used to define a file pointer for use in file operations. Before we can write to a file, we must open it. This means that we must tell the system that we want to write to a file and what the file name is. We do this with the ‘fopen()’ function. The file pointer, ‘fp’ points to the file and two arguments are required, the file name first, followed by the file type. The file name is any valid DOS file name, and can be expressed in upper or lower case letters. It is enclosed in double quotes. The second parameter is the file attribute and can be any of three letters, r(reading), w(writing), or a(appending), and must be lower case.

Reading (r):
When an ‘r’ is used, the file is opened for reading, a ‘w’ is used to indicate a file to be used for writing, and an ‘a’ indicates that you desire to append additional data to the data already in an existing file. Using the ‘r’ indicates that the file is assumed to be a text file. Opening a file for reading requires that the file already exist. If it does not exist, the file pointer will be set to NULL. Demo program to open a file:

#include<stdio.h> main() { FILE *fopen(), *fp; int i; fp = fopen("adil.txt","r"); i = getc(fp) ; while (i!= EOF) /*EOF = end of file*/ { putchar(i); i = getc(fp); } fclose(fp); }

txt". /*writes data to the file*/ fclose(fp).h> main() { FILE *fp. Using the w indicates that the file is assumed to be a text file. /*append some text*/ fclose(fp).txt". the data input point will be positioned at the end of the present data so that any new data will be added to any data that already exists in the file."a"). resulting in the deletion of any data already there. fprintf(fp. it will be created if it does not already exist and it will be reset if it does. return 0. Using the ‘a’ indicates that the file is assumed to be a text file.Writing (w): When a file is opened for writing."Welcome to the world of C")."w"). file = fopen("adil123."This is just an example :)"). Demo program to write a file. #include <stdio."%s"."%s". } . } Appending (a): When a file is opened for appending. #include <stdio. it will be created if it does not already exist and it will be initially empty. fprintf(fp.h> main() { FILE *fp file = fopen("adil123. Demo program to append a file. If it does exist. return 0.

then close it. Closing a file To close a file you simply use the function ‘fclose’ with the file pointer. /\ ____/_ \____ \ ___\ \ / \/ / \/ / / /\__/_/\ . In the Demo program. and in fact is identical to. ‘fprintf’ replaces our familiar ‘printf’ function name. After running it. and open it again for appending. or you can use a different one. look at your directory for a file named “adil123. but it is good programming practice for you to close all files in spite of the fact that they will be closed automatically. The file pointer is simply a tool that you use to point to a file and you decide what file it will point to. etc. close it. you will not get any output to the monitor because it doesn’t generate any. that is where your output will be. it is not necessary to close the file because the system will close all open files before returning to DOS. Actually. When you run it. and reopen it for reading.Outputting to the file The job of actually outputting to the file is nearly identical to the outputting we have already done to the standard output device. You can open a file for writing. The only real differences are the new function names and the addition of the file pointer as one of the function arguments. Each time you open it. The remainder of the statement looks like. and the file pointer defined earlier is the first argument. in this demo program. Compile and run this program. you could use the same file pointer. the ‘printf’ statement.txt” and type it. because that would act as a reminder to you of what files are open at the end of each program.

/__\ \_____\ \ / \/ .