Sunday, March 14, 2021

CH 10:File Handling

 The random access memory is volatile and its content is lost once the program terminates. In order to persist the data forever, we use files.

A file data stored in a storage device. A C program can talk to the file by reading content from it and writing content to it.


File pointer

The “File” is a structure that needs to be created for opening the file. A file pointer is a pointer to this structure of the file.

File pointer is needed for communication between the file and the program.

A file pointer can be created as follows:

FILE *ptr;
ptr=fopen(“filename.ext”,”mode”);

File opening modes in C

C offers the programmers to select a mode for opening a file.

Following modes are primarily used in c File I/O


Types of Files

There are two types of files:

  1. Text files(.txt, .c)
  2. Binary files(.jpg, .dat)

Reading a file

A file can be opened for reading as follows:

FILE *ptr;
ptr=fopen(“Harry.txt”,”r”);
int num;

Let us assume that “Harry.txt” contains an integer

We can read that integer using:

fscanf(ptr,”%d”,&num);                => fscanf is file counterpart of scanf

This will read an integer from the file in the num variable.

Quick Quiz: Modify the program above to check whether the file exists or not before opening the file.


Closing the file

It is very important to close file after read or write. This is achieved using fclose as follows:

fclose(ptr);

This will tell the compiler that we are done working with this file and the associated resources could be freed.


Writing to a file

We can write to a file in a very similar manner as we read the file

FILE *fptr;
fptr=fopen(“Harry.txt”,”w”);
int num=432;
fprintf(fptr,%d”,num);
fclose(fptr);

fgetc() and fputc()

fgetc and fputc are used to read and write a character from/to a file.

fgetc(ptr);                          => Used to read a character from file

fputc(‘c’,ptr);                     => Used to write character 'c' to the file


EOF: End of File

fgetc returns EOF when all the characters from a file have read. So we can write a check like below to detect the end of file.

while(1){
ch=fgetc(ptr);	   // When all the content of a file has been read, break the loop
if(ch==EOF){
break;
}
//code
}
Programs For Practice
  1. Write a program to read three integers from a file.
  2. Write a program to generate a multiplication table of a given number in text format. Make sure that the file is readable and well-formatted.
  3. Write a program to read a text file character by character and write its content twice in a separate file.
  4. Take name and salary of two employees as input from the user and write them to a text file in the following format:

        name1, 3300

        name2, 7700

  1. Write a program to modify a file containing an integer to double its value.

       If old value = 2, then new file value = 4

No comments:

Post a Comment

Decision Making in C / C++

Decision Making in C / C++ (if , if..else, Nested if, if-else-if )   There come situations in real life when we need to make some decisions ...