File in C Programming: Why Files Are Needed?
File in C Programming: Why Files Are Needed?
Types of Files
When dealing with files, there are two types of files you should know about:
1. Text files
2. Binary files
1. Text files
Text files are the normal .txt files that you can easily create using Notepad or any simple text editors.
When you open those files, you'll see all the contents within the file as plain text. You can easily edit or delete
the contents.
They take minimum effort to maintain, are easily readable, and provide least security and takes bigger storage
space.
2. Binary files
Instead of storing data in plain text, they store it in the binary form (0's and 1's).
They can hold higher amount of data, are not readable easily and provides a better security than text files.
File Operations
In C, you can perform four major operations on the file, either text or binary:
FILE *fptr;
ptr = fopen("fileopen","mode")
For Example:
fopen("E:\\cprogram\\newprogram.txt","w");
fopen("E:\\cprogram\\oldprogram.bin","rb");
Closing a File
The file (both text and binary) should be closed after reading/writing.
int main()
{
int num;
FILE *fptr;
fptr = fopen("C:\\program.txt","w");
fprintf(fptr,"%d",num);
fclose(fptr);
return 0;
}
printf("Enter a sentence:\n");
gets(sentence);
fprintf(fptr,"%s", sentence);
fclose(fptr);
return 0;
}
Output
Enter sentence:
9. {
10. printf("Error! opening file");
11. // Program exits if file pointer returns NULL.
12. exit(1);
13. }
14.
15. // reads text until newline
16. fscanf(fptr,"%[^\n]", c);
17.
18. printf("Data from the file:\n%s", c);
19. fclose(fptr);
20.
21. return 0;
22. }
#include <stdio.h>
int main()
{
char name[50];
int marks, i, num;
FILE *fptr;
fptr = (fopen("C:\\student.txt", "w"));
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
fclose(fptr);
return 0;
}
#include <stdio.h>
int main()
{
char name[50];
int marks, i, num;
FILE *fptr;
fptr = (fopen("C:\\student.txt", "a"));
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
fclose(fptr);
return 0;
}
#include <stdio.h>
struct student
{
char name[50];
int height;
};
int main(){
struct student stud1[5], stud2[5];
FILE *fptr;
int i;
fptr = fopen("file.txt","wb");
for(i = 0; i < 5; ++i)
{
fflush(stdin);
printf("Enter name: ");
gets(stud1[i].name);
Open for reading in binary mode. If the file does not exist, fopen( )
rb
returns NULL.
Open for writing in text mode. If the file exists, its contents are
w overwritten. If the file doesn’t exist, a new file is created. Returns
NULL, if unable to open the file.
Open for writing in binary mode. If the file exists, its contents are
wb
overwritten. If the file does not exist, it will be created.
Open for append in binary mode. Data is added to the end of the
ab
file. If the file does not exist, it will be created.
Open for both reading and writing in binary mode. If the file does
rb+
not exist, fopen( ) returns NULL.
Searches file. If the file exists, its contents are overwritten. If the file
w+ doesn’t exist a new file is created. Returns NULL, if unable to open
the file.
Open for both reading and writing in binary mode. If the file exists,
wb+ its contents are overwritten. If the file does not exist, it will be
created.
Open for both reading and appending in binary mode. If the file does
ab+
not exist, it will be created.