written 2.9 years ago by | modified 2.9 years ago by |
What are various types of files? What are the various modes in which a file can be opened? Explain by giving examples.
written 2.9 years ago by | modified 2.9 years ago by |
What are various types of files? What are the various modes in which a file can be opened? Explain by giving examples.
written 2.9 years ago by |
The Random Access Memory is volatile and its content is lost once the program terminates. In order to persist the data format forever we use files.
A file is a data data stored in a storage device. A C program can talk to the file by reading content from it and writing content to it.
Types of Files :-
Let's discuss it -
FILE pointer :- A 'FILE' is a structure which needs to be created for opening the file. A FILE pointer is a pointer to the structure of the file. A FILE pointer is needed for communication between the files and the program.
A FILE pointer can be crated as follows :-
FILE *ptr;
ptr = fopen("filename.txt", "mode");
Files opening modes in C :-
Some of the primarily used opening modes in C are -
Reading a file : -
A file can be opened for reading as follows :-
FILE *ptr;
ptr = fopen("abc.txt", "r");
int num;
Let us assume that the "abc.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 form file in num variable.
Closing the file : -
It is very very important to close the 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 resource could be freed.
Writing to a file : -
We can write to a file in a very similar manner like we read the file.
FILE *ptr;
fptr = fopen("abc.txt", "w");
int num = 465;
fprintf(fptr, "%d", num);
fclose(fptr);
fgetc() and fputc() : -
fgetc and fputc are used to write and read a character to / from a file.
fgetc(ptr); //Used to read a character from a file.
fputc('c', ptr); //Used to write a character 'c' to a file.
EOF : End of the file : -
fgetc returns EOF when all the characters from a file have been read. So we can write a check like below to detect the end of the file.
while(1){
ch = fgetc(ptr);
if(ch == EOF){
break;
}
//------
//Code
//------
}
When all the content of the file has been read, it will break the loop !
For better understanding, let's take an example.
Suppose, We have to write a program in C to create and store information in a text file.
The code for the same will be -
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[1000];
FILE *fptr;
char fname[20]="test.txt";
printf("\n\n Create a file (test.txt) and input text :\n");
printf("----------------------------------------------\n");
fptr=fopen(fname,"w");
if(fptr==NULL)
{
printf(" Error in opening file!");
exit(1);
}
printf(" Input a sentence for the file : ");
fgets(str, sizeof str, stdin);
fprintf(fptr,"%s",str);
fclose(fptr);
printf("\n The file %s created successfully...!!\n\n",fname);
return 0;
}