To Open a File:
The syntax for opening the file is: File* fopen(char* name, char* mode); Opens the file 'name' and returns the FILE pointer. Values for 'mode' are 'r','w'and 'a'. fopen returns NULL if its unable to open file.
1. Presented by
Rifatu Jaman
Id: 222015032
Adnan al-emran ontor
Id: 222015033
1
Presented To
MR. S. M. RASHIDUL HASAN NIJHUM
Lecturer of GUB
start
Topic: File operation in C
programming
2. 2
Instructions
Why files are needed?
• When a program is terminated, the entire data is lost. Storing
in a file will preserve your data even if the program terminates.
• If you have to enter a large number of data, it will take a lot of
time to enter them all.
However, if you have a file containing all the data, you can easily
access the contents of the file using a few commands in C.
• You can easily move your data from one computer to another
without any changes.
File operation
3. 3
Types
Types of Files
When dealingwith files, there are two types of files you
should know about:
1
Text files
2
Binary files
File operation
4. 4
File Operations
In C, you can perform four major operations on files, either text or
binary:
1.Creating a new file
2.Opening an existing file
3.Closing a file
4.Reading from and writing information to a file
File Operations
File operation
Declaration of file
-------- ----------------------
FILE *name_of_file;
File Declaration
5. 5
Opening Modes in Standard I/O
Mode Meaning of Mode During Inexistence of file
r Open for reading. If the filedoes not exist, fopen() returns NULL.
rb
Open for reading in binary mode.
If the filedoes not exist, fopen() returns NULL.
w
Open for writing.
If the fileexists, its contents are overwritten.
If the filedoes not exist, it willbe created.
wb
Open for writing in binary mode.
If the fileexists, its contents are overwritten.
If the filedoes not exist, it willbe created.
a Open for append.
Data is addedto the end of the file.
If the filedoes not exist, it willbe created.
ab Open for append in binary mode.
Data is addedto the end of the file.
If the filedoes not exist, it willbe created.
r+ Open for both reading and writing. If the filedoes not exist, fopen() returns NULL.
rb+ Open for both reading and writing in binary mode. If the fileexists, its contents are overwritten.
If the filedoes not exist, it willbe created.
w+ Open for both reading and writing. If the fileexists, its contents are overwritten.
If the filedoes not exist, it willbe created.
wb+ Open for both reading and writing in binary mode. If the fileexists, its contents are overwritten.
a+ Open for both reading and appending. If the filedoes not exist, it willbe created.
ab+ Open for both reading and appendingin binary
mode.
If the filedoes not exist, it willbe created.
6. 6
Opening a file
The basic file operations are
fopen - open a file
fclose - close an opened file
fread - read from a file
fwrite - write to a file
fseek/fsetpos - move a file pointer to somewhere in a
file.
ftell/fgetpos - tell you where the file pointer is
located.
7. 7
fputc()
The fputc() function is used to write a single character into file. It outputs a
character to a stream.
Syntax:
int fputc(int c, FILE *stream)
fputc()
File operation
Example:
#include <stdio.h>
main(){
FILE *fp;
fp = fopen("file1.txt", "w"); //opening file
fputc('a',fp); //writing single character into file
fclose(fp); //closing file
}
file1.txt
a
8. 8
fgetc()
The fgetc() function returns a single character from the file. It gets a character
from the stream. It returns EOF at the end of file.
Syntax:
int fgetc(FILE *stream)
fgetc()
File operation
#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
char c;
fp=fopen("myfile.txt","r");
while((c=fgetc(fp))!=EOF){
printf("%c",c);
}
fclose(fp); //means file close
}
myfile.txt
green
university
this is simple text message
Output :
green university
9. fseek()
Random Access to file in C
• Random access means we can move to any part of a file and read or write
data from it without having to read through the complete file. We can access
the data stored in the file in two ways.
• Sequentially
• Randomly
Example: fseek(), ftell(), rewind()
File operation 9