Files Management: C Programming Unit 5
Files Management: C Programming Unit 5
C PROGRAMMING UNIT 5
Introduction 2
So far we dealt with programs that store and process data in the computer’s primary
memory(RAM). How do we do the same with data in the secondary memory(disk).
Data is stored in disks in the form of files. E.g. Your notepad files, word files, movie or
music files etc.
Files are generally categorized into two: text files and binary files.
Notepad file(notes.txt) or your program source code is text file where readable text is
stores. Video files, music files, executable program files etc. are examples of binary files.
3
Following are the most common operations on files
o Open/Create
o Read
o Write
o Close
C provides a number of functions that helps to perform basic file
operations.
4
Defining and Opening File(fopen( )) 5
C provides a data type to work with files i.e. FILE. We need to create pointer of FILE
type before opening file.
Function used create/open file is fopen(). This function on successful execution returns a
pointer to the opened file.
fopen() takes two parameters, 1. The filename 2. The mode in which file is to
opened(more about it later)
1. File name :
Normally file name consists of the name part and an extension( e.g.
hello.txt). Extension is optional normally.
2. Mode:
mode specifies the purpose of opening the file. 8
Three major modes of opening a file : Read, Write, Append.
1. r: opens a text file in reading mode. You can only read data from file. The
file has to exist for this. Otherwise NULL is returned to indicate error.
2. w: Create and opens a text file in writing mode if it is not existing. If file
already exists, its contents are destroyed and opens it for writing fresh
content.
3. a: Opens a text file in append mode if it exists. Starts writing at the
end(append). Existing data is not destroyed. If the file does not exist, it
creates one and open for appending data(from start).
9
There are three additional modes which are variants of the above:
1. r+ opens an existing text file in both reading and writing mode. Start
reading/writing at the beginning.
2. w+ Creates new file if not existing. Opens a text file in both reading and
writing mode. Destroys the contents of existing file.
3. a+ Creates new file if not existing. Opens the file in both reading and
writing mode (append). Contents of existing file are not destroyed.
10
E.g.
FILE *p1, *p2;
p1 = fopen("data","r");
p2 = fopen("results.txt","w");
Closing a file (fclose()) 11
After we are finished with all operations, we are expected to close the file.
fclose () is the function/ for this. It takes file handle as the parameter.
E.g.: fclose(p1);
fclose(p2);
Input/Output Operations on File 12
getc() and putc()fuctions:
The two simplest functions for I/O operations on files are: getc() &putc()
Function putc(): Write a single character into the file specified.
General Syntax: putc(c,fp);
where c is a character variable and fp, the file pointer to the file opened in
write mode.
Function getc(): Read a character from file specified and returns it.
General Syntax : c = getc(fp);
where c is a character variable and fp is file pointer to opened file.
#include <stdio.h>
int main() {
13
FILE *f1;
char c,d;
f1 = fopen("INPUT1", "w"); /* Open the file INPUT for writing*/
printf("Enter a character:");
c = getchar(); /* Get a character from keyboard */
putc(c,f1); /* Write into the file. */
fclose(f1); /* Close the file INPUT */
f1 = fopen("INPUT1","r"); /* Reopen the file INPUT1 */
d = getc(f1); /* Read back the character from file*/
printf("Data Output:");
printf("%c",d); /* Display a character on screen */
fclose(f1); /* Close the file*/
return 0; }
getw() and putw() functions 14
Function putw(): Write a single integer into the file specified.
General Syntax: putw(number,fp);
where number is a integer variable and fp, the file pointer to the file opened in
write mode.
Function getw(): Read a character from file specified and returns it.
General Syntax : num = getw(fp);
Where num is a integer variable and fp is file pointer to opened file.
#include <stdio.h>
int main() {
FILE *f1;
15
int number, num;
printf("Enter a integer:"); Output:
scanf("%d",&num); Enter ainteger:1234
// Open file for reading File data is: 1234
f1 = fopen("DATA1", "w"); /* Create DATA file */
// Write the integer to the file
putw(num,f1);
//closing the file
fclose(f1);
// Open the file for reading
f1 = fopen("DATA1", "r");
number = getw(f1); // Read the integer from file
printf("File data is: %d",number);
return 0; }
fprintf() and fscanf() functions 16
Function fprintf(): Write specified formatted data into file
General Syntax: fprintf(fp , “control string”, data list);
where fp is the file pointer, control string is similar to printf function (“ e.g.:
%d %f %s”) and data list is the list of data item to be written to file.
Need to use address operator for single data item , as in case of scanf().
E.g. fscanf(f1, “%s %d”, item, &quantity);
#include <stdio.h> fp = fopen(filename, "r");
printf("Enter a string , integer & float:\n"); File data is: Hello 1234 12.450000