Unit-5(Part-3)File Handling in C
Unit-5(Part-3)File Handling in C
Unit-5
File Handling in C
WHAT IS FILE?
File is a collection of bytes that is stored on secondary storage devices like disk. There are two kinds of files
in a system. They are,
File handling in C enables us to create, update, read, and delete the files stored on the local file system
through our C program. The following operations can be performed on a file.
1
Unit-5(Part-3)File Handling in C
2
Unit-5(Part-3)File Handling in C
o The file name (string). If the file is stored at some specific location, then
we must mention the path at which the file is stored. For example, a file
name can be like "c://some_folder/some_file.ext".
o The mode in which the file is to be opened. It is a string.
Mode Description
Syntax:
#include <stdio.h>
main()
{
FILE *fp;
fp = fopen("file.txt", "w");//opening file
fprintf(fp, "Hello file by fprintf...\n");//writing data into file
fclose(fp);//closing file
}
Syntax
#include <stdio.h>
main(){
FILE *fp;
char buff[255];//creating char array to store data of file
fp = fopen("file.txt", "r");
while(fscanf(fp, "%s", buff)!=EOF){
printf("%s ", buff );
}
fclose(fp);
}
Output:
5
Unit-5(Part-3)File Handling in C
}
fclose(fp);
}
#include <stdio.h>
void main()
{
FILE *fptr;
int id;
char name[30];
float salary;
fptr = fopen("emp.txt", "w+");/* open for writing */
if (fptr == NULL)
{
printf("File does not exists \n");
return;
}
printf("Enter the id\n");
scanf("%d", &id);
fprintf(fptr, "Id= %d\n", id);
printf("Enter the name \n");
scanf("%s", name);
fprintf(fptr, "Name= %s\n", name);
printf("Enter the salary\n");
scanf("%f", &salary);
fprintf(fptr, "Salary= %.2f\n", salary);
fclose(fptr);
}
6
Unit-5(Part-3)File Handling in C
Output:
Enter the id
1
Enter the name
sonoo
Enter the salary
120000
Now open file from current directory. For windows operating system, go to TC\bin
directory, you will see emp.txt file. It will have following information.
Syntax:
Syntax:
7
Unit-5(Part-3)File Handling in C
#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
char c;
clrscr();
fp=fopen("myfile.txt","r");
while((c=fgetc(fp))!=EOF){
printf("%c",c);
}
fclose(fp);
getch();
}
C fputs() and fgets()
The fputs() and fgets() in C programming are used to write and read string
from stream. Let's see examples of writing and reading file using fgets() and
fgets() functions.
Syntax:
fp=fopen("myfile2.txt","w");
fputs("hello c programming",fp);
8
Unit-5(Part-3)File Handling in C
fclose(fp);
getch();
}
myfile2.txt
hello c programming
Syntax
#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
char text[300];
clrscr();
fp=fopen("myfile2.txt","r");
printf("%s",fgets(text,200,fp));
fclose(fp);
getch();
}
Output:
hello c programming
Output:
Enter character: a
9
Unit-5(Part-3)File Handling in C
Character entered: a
1) EOF:
EOF stands for End of File. The function getc() returns EOF, on success.
2) putw() function:
The putw() function is used to write integers to the file.
The putw() function takes two arguments, first is an integer value to be written to
the file and second is the file pointer where the number will be written.
3) getw() function:
The getw() function is used to read integer value form the file.
10
Unit-5(Part-3)File Handling in C
The getw() function takes the file pointer as argument from where the integer value
will be read and returns returns the end-of-file if it has reached the end of file.
4) fscanf() function:
The fscanf() function is used to read set of characters from file. It reads a
word from the file and returns EOF at the end of file.
Syntax:
int fscanf(FILE *stream, const char *format [, argument, ...])
Example:
#include <stdio.h>
main(){
FILE *fp;
char b[255];//creating char array to store data of file
fp = fopen("file.txt", "r");
while(fscanf(fp, "%s", b)!=EOF){
printf("%s", b);
}
fclose(fp);
}
5) fprintf() function:
The fprintf() function is used to write set of characters into file. It sends
formatted output to a stream.
Syntax:
int fprintf(FILE *stream, const char *format [, argument, ...])
Example:
#include <stdio.h>
main(){
FILE *fp;
fp = fopen("file.txt", "w");//opening file
fprintf(fp, "Hello file by fprintf...\n");//writing data into file
fclose(fp);//closing file
}
Output:
Hello file by fprintf...
11
Unit-5(Part-3)File Handling in C
#include <stdio.h>
void main()
{
FILE *fptr;
int id;
char name[30];
float salary;
fptr = fopen("emp.txt", "w+");/* open for writing */
if (fptr == NULL)
{
printf("File does not exists \n");
return;
}
printf("Enter the id\n");
scanf("%d", &id);
fprintf(fptr, "Id= %d\n", id);
printf("Enter the name \n");
scanf("%s", name);
fprintf(fptr, "Name= %s\n", name);
printf("Enter the salary\n");
scanf("%f", &salary);
fprintf(fptr, "Salary= %.2f\n", salary);
fclose(fptr);
}
Output:
Enter the id
1
Enter the name
sonoo
Enter the salary
120000
12
Unit-5(Part-3)File Handling in C
13
Unit-5(Part-3)File Handling in C
fptr=fopen("t4.txt","r");
if(fptr==NULL)
{
printf("\n File not available");
getch();
exit(0);
}
printf("\nContents of the File is:");
while((ch=fgetc(fptr))!=EOF) {
printf("%c",ch);
}
fclose(fptr);
getch();
}
3) /*C program to read integer numbers from a file named DATA and
then write all odd numbers to a file named ODD and all even
numbers to a file named EVEN*/
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int a,n,i;
FILE *fp1,*fp2,*fp3;
clrscr();
fp1=fopen("DATA.txt","w");
if(fp1==NULL)
{
printf("File could not open!!");
getch();
exit(0);
}
14
Unit-5(Part-3)File Handling in C
for(i=0;i<n;++i)
{
scanf("%d",&a);
15
Unit-5(Part-3)File Handling in C
putw(a,fp1);
}
fclose(fp1);
fp1=fopen("DATA.txt","r");
fp2=fopen("ODD.txt","w");
fp3=fopen("EVEN.txt","w");
if(fp1==NULL||fp2==NULL||fp3==NULL)
{
printf("File could not open!!");
getch();
exit(0);
}
while((a=getw(fp1))!=EOF)
{
if(a%2!=0)
putw(a,fp2);
else
putw(a,fp3);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
fp2=fopen("ODD.txt","r");
fp3=fopen("EVEN.txt","r");
if(fp2==NULL||fp3==NULL)
{
printf("File could not open!!");
getch();
exit(0);
}
16
Unit-5(Part-3)File Handling in C
fclose(fp2);
fclose(fp3);
getch();
}
Output:
How many numbers?10
Enter contents of DATA file:
1
2
3
4
5
6
7
8
9
10
17
Unit-5(Part-3)File Handling in C
void main()
{
FILE *fp1,*fp2;
char ch,f1[50],f2[50];
clrscr();
fp1=fopen(f1,"r");
fp2=fopen(f2,"w");
if(fp1==NULL||fp2==NULL)
{
printf("File could not open!!");
getch();
exit(0);
}
fclose(fp1);
fclose(fp2);
printf("\n File copied successfully");
getch();
}
18