Files
Files
Till now, we were dealing with data associated with the programs
in the console/terminal with the help of console oriented I/O
functions such as printf() and scanf() functions.
This data can be an input to the program or it can be an output of
the program.
Handling data in the console becomes difficult as the size of the
data increases because in console oriented I/O operations the data
is lost as soon as the console/terminal is closed or the program
execution is completed or the computer is turned off.
Therefore, in order to retain such data instead of loosing it in the
terminal a FILE can be used.
What is a File?
A file is a place on the disk where a group of related data
is stored.
A file is a collection of data stored on a secondary
storage device such as hard disk.
Hello world!
Using Files in C
To use files in C, we must use the following steps,
1. Declare a File pointer variable
2. Open the file (in any one of the modes- r w a)
3. Process the file (read/write)
4. Close the file
1. Declare a File pointer variable
In order to perform any operations on the file we must first
have an access to the files location(address).
This is done by creating a pointer pointing to the FILE
structure (defined in stdio.h). This is accomplished by declaring
a FILE pointer.
Syntax for declaring a FILE pointer:
FILE *file_pointer_name;
An Example,
FILE *fp;
2. Opening the file
A file must be opened before data can be read from or
written to it.
In order to open a file and associate it with a stream, the
fopen() function is used.
fopen() function returns a pointer to the structure on
successful opening of the file and if it fails it returns NULL.
Syntax for opening a file: r for reading
w for writing
FILE *fp; a for appending
fp = fopen(“filename”,“mode”);
FILE *fp;
An example,
fp = fopen(“sample.txt”,“w”);
2. Opening the file
If the file is in the current working directory, then just
name of the file is enough while opening it.
Example: fopen(“sample.txt”, “r”);
However, full path of the files location should be given if
it is not in the current working directory.
Example: fopen(“D:\\Programs\\sample.txt”, “r”);
r Opens the file for reading. If the file does not exist, fopen() returns NULL.
w Opens the file for writing. If the file exists, its contents are overwritten.
If the file does not exist, a file will be created.
Open for append.
a Data is added to the end of
the file.
If the file does not exist, a new file will be
created.
Syntax:
fclose(FILE *fp);
Example, fclose(fp);
Reading data from a file using fgetc():
fgetc() is used to read one character at a time from a file.
fgetc() returns the character as an int or return EOF to indicate an error or
end of the file. Example:
Syntax: FILE *fp;
int fgetc(FILE *stream); char ch;
fp = fopen(“student.txt”, “r”);
ch = fgetc(fp);
Writing data to a file using fputc():
fputc() is used to write one character(a byte) to the file.
fputc() returns the character value that it has written on success or EOF in
case of error. Example:
Syntax: FILE *fp;
int fputc(int c, FILE *stream); char ch= ‘A’;
fp = fopen(“student.txt”, “w”);
fputc(ch, fp);
Reading data from a file using fgets():
fgets() function reads at most one less than the number of characters
specified by size from the given stream.
fgets() terminates as soon as it encounters either a newline/EOF/or error.
Example:
Syntax:
FILE *fp;
char fgets(char *s, int size, FILE *stream); char s[10];
fp = fopen(“student.txt”, “r”);
fgets(s, 10, fp);
Syntax:
int fprintf(FILE *stream, “format specifiers”, variables);
Example:
fprintf(fp, “%d%s”, &rollno, name);
As we know fgetc()
reads one character at
a time from the file
wherever fp is pointing
to. So a loop is used to
repeatedly read the
characters one after
the other and printed
using printf()
Output:
Write a C program to copy the contents of one file to another file.
Write a C program to create a file named myfile and write content that the
user types from the keyboard till the user enters 0.
Output:
Write a C program that creates a file reading contents that the user types from the
keyboard till EOF. The text in this file must be in lowercase. There could be
multiple blanks in between some words. Create another file in which the same
content is copied in UPPERCASE and with only one blank in between the words
that contained multiple blanks.