Handling Files in C
Handling Files in C
Semester 2
Module V
Topic : File Handling in C
Prepared by
Ms. Amitha Mathew
Assistant Professor
Department of Computer Science and Engineering
Contents
Introduction to Files
File Pointers
Opening a file
Closing a file
Example Programs
2 File Handling in C
Introduction to Files
When a computer reads a file, it copies the file from the storage device to
memory; when it writes to a file, it transfers data from memory to
the storage device.
3 File Handling in C
Introduction to Files cont’d
Types of files:
4 File Handling in C
Introduction to Files cont’d
EOF
It's a sign that the end of a file is reached, and that there will be no data
anymore.
On Linux systems the character to input to cause an EOF is CTRL+D. For
5 File Handling in C
Steps in processing a file in C
6 File Handling in C
File pointer
C uses a ‘structure’ called FILE (defined in stdio.h) to store the attributes
of a file.
The FILE structure contains information about the file being used such as
it’s current size, it’s location in memory , a character pointer that points to
the character that is about to read.
Example
FILE *fp;
fp=fopen(“File1.txt”, “r”);
8 File Handling in C
File access modes
Mode Meaning
r Open Text File in read mode
If the file exists, the file pointer is positioned at the beginning
If file doesn't exist, NULL is returned
w Open Text File in write mode
If the file exists, the contents of the file is erased
If file doesn't exist, new file is created
9 File Handling in C
File access mode
10 File Handling in C
Other Access modes
Mode Meaning
r+ Open Text File in read/write mode
If the file exists, the file pointer is positioned at the beginning
If file doesn't exist, returns a null
w+ Open Text File in write/read mode
If the file exists, the contents of the file is erased
If file doesn't exist, new file is created
New contents can be written and can read the contents written
a+ Open Text File for reading and
If the file exists, the file pointer is positioned at the end and data is written at the
end of the file
If file doesn't exist, a new file is created and new data can be written into it
rb Open File in read only mode in binary format
wb Open File in write only mode in binary format
ab Open File in append only mode in binary format
11 File Handling in C
Read /Write data to file
12 File Handling in C
Read /Write data to file
Read one character at a time Write one character at a time
getc() putc()
Syntax: Syntax:
variable=getc(filepointer); putc(variable,filepointer);
Example: Example:
FILE *fp; FILE *fp;
char ch; char ch=‘a’;
fp=fopen(“file1.txt”, “r”); fp=fopen(“file1.txt”, “w”);
ch=getc(fp); putc(ch,fp);
fclose(fp); fclose(fp);
13 File Handling in C
Read /Write data to file
Read one integer at a time Write one integer at a time
getw() putw()
Syntax: Syntax:
variable=getw(filepointer); putw(variable,filepointer)
Example: Example:
FILE *fp; FILE *fp;
int n; int n=10;
fp=fopen(“file1.txt”, “r”); fp=fopen(“file1.txt”, “w”);
n=getw(fp); putw(n,fp);
fclose(fp); fclose(fp);
14 File Handling in C
Read /Write data to file
Read one line at a time Write one line at a time
fgets() fputs()
Syntax: Syntax:
fgets(stringname, no of characters, filepointer); fputs(stringname, filepointer);
Example: Example:
FILE *fp;
FILE *fp;
char str[100];
char str[100];
gets(str);
fp=fopen(“file1.txt”, “r”); fp=fopen(“file1.txt”, “w”);
fgets(str,10,fp); fputs(str,fp);
fclose(fp); fclose(fp);
15 File Handling in C
Read /Write data to file
Reading assorted type of data at a Writing assorted type of data at a
time time
fscanf() fprintf()
Syntax: Syntax:
fscanf(fp, “control string”, variable list); fprintf(fp, “control string”, variable list);
Example: Example:
fscanf(f1, “%s%d”, item, &quantity); fprintf(f1, “%s%d”, name, age);
16 File Handling in C
Closing a File
When we finish file operation with a mode, we should close the file , before
ending the program or opening the same file in another mode .
Syntax
fclose(filepointer);
Example:
fclose(fp);
17 File Handling in C
Example 1:Read each character from a file and
print it on output screen
#include <stdio.h>
void main()
{
char ch;
FILE *fp;
fp=fopen(“file1.txt","r");
while((ch=getc(fp) ) !=EOF)
{
printf("%c",ch);
}
fclose(fp);
}
18 File Handling in C
File Handling in C
File Handling in C
Ince
File Handling in C
Example 2: Writing data to file
#include <stdio.h>
void main()
{
char ch;
FILE *fp;
fp=fopen("file2.txt","w");
while((ch=getchar()) !=EOF)
{
putc(ch,fp);
}
fclose(fp);
}
22 File Handling in C
File Handling in C
File Handling in C
Example 3: Appending data to file
#include <stdio.h>
void main()
{
char ch;
FILE *fp;
fp=fopen("file2.txt","a");
while((ch=getchar()) !=EOF)
{
putc(ch,fp);
}
fclose(fp);
}
25 File Handling in C
File Handling in C
File Handling in C
Example:4 Using fprintf() to write data to file
#include <stdio.h>
void main()
{
char name[50];
int mark,i,n;
FILE *fp;
printf("Enter the number of students");
scanf("%d",&n);
fp=fopen("student.txt","w");
printf("Enter Details of %d Students\n",n);
for (i=0;i<n;i++)
{
printf("Enter Name");
scanf("%s",name);
printf("Enter Mark");
scanf("%d",&mark);
fprintf(fp,"Name:%s Mark:%d\n",name,mark);
}
fclose(fp);
28 File Handling in C
}
File Handling in C
File Handling in C
Example:5 Using fscanf() to read data from file
#include <stdio.h>
#include <stdio.h> void main()
void main() {
{ char name[50];
char name[50]; int mark,i,n,ret;
int mark,i,n; FILE *fp;
FILE *fp; fp=fopen("student.txt","r");
fp=fopen("student.txt","r"); while(1)
while((fscanf(fp,"%s%d",name,&mark))!=EOF) {
ret=fscanf(fp,"%s%d",name,&mark);
{
if(ret==EOF)
printf("%s\t: %d\n",name,mark);
break;
}
else
printf("%s\t: %d\n",name,mark);
fclose(fp); }
} fclose(fp);
31 File Handling in C
}
File Handling in C
File Handling in C
Do It Yourself
Write a program to read the contents of the file named file1.txt and
merge it with the contents of the file named file2.txt
Hint: Read one by one each character from file1.txt and append it to
file2.txt
34 File Handling in C
Appending contents one of file to another file
#include <stdio.h>
void main()
{
char ch;
FILE *fp1,*fp2;
fp1=fopen("file1.txt","r");
fp2=fopen("file2.txt","a");
while((ch=getc(fp1) ) !=EOF)
{
putc(ch,fp2);
}
fclose(fp1);
fclose(fp2);
}
35 File Handling in C