8/10/2019 Chapter 14 Files .pptx
1/20
Munindra Lunagaria
Files
Computer Programming and Utilization
Prof. Munindra Lunagaria
MEFGI, India
Files
8/10/2019 Chapter 14 Files .pptx
2/20
Munindra Lunagaria
A file is nothing but a collection of records and each line of the data is recorded in
the file.
The record is a group of related data items.
The library functions to access the files are available in stdio.h
Introduction
8/10/2019 Chapter 14 Files .pptx
3/20
Munindra Lunagaria
The reading and writing of data is done with streams.
A stream is a file and using physical device like keyboard information is stored inthe file.
Printer and monitor are used to display the stream.
The FILE object uses these devices.
The FILE object contains all the information about a stream like current position,pointer to any buffer, error and end of file (EOF).
Streams and File Types
8/10/2019 Chapter 14 Files .pptx
4/20
Munindra Lunagaria
Sequential File: Here, data are arranged sequentially.
To read the last record of the file it is expected to read all the
records before it.
It takes more time to access the records.
Random Access File: In this type, data can be read and
modified randomly without reading the intermediate
records.
File Types
8/10/2019 Chapter 14 Files .pptx
5/20
Munindra Lunagaria
Opening a File
Opening of file creates a link between operating system and file
functions
FILE *fp;fp=fopen(data.txt,r);
fopen()performs following tasks:
Searches the disk for opening of file
If file exists, loads the file from disk to memory
If file does not exist, returns NULL
Locates a character pointer pointing to first character of file
8/10/2019 Chapter 14 Files .pptx
6/20
Munindra Lunagaria
Reading or Writing a File
The fgetc() function is used to read the contents of the file
ch=fgetc(fp);
The fputc()function is used to write the contents in the file.ch=fputc(c,fp);
It reads and write the character from and in the current pointerposition and advances the pointer position so that the next character is
pointed.
8/10/2019 Chapter 14 Files .pptx
7/20Munindra Lunagaria
Closing the file enables to wash out all its contents from
RAM buffer, and further link is disconnected from the file.
To close a single file:
fclose(fp);
To close all opened files
fcloseall();
It returns the number of files that have been closed.
Closing a File
8/10/2019 Chapter 14 Files .pptx
8/20Munindra Lunagaria
Write a Program on Opening and Closing the File
# include
# include # include
void main()
{
FILE *fp;
char c= ;
clrscr();
fp=fopen(data.txt,w);
if(fp==NULL)
{
printf(Can not open file);
exit(1);}
printf(Write data & to stop
press .:);
while(c!=.){
c=getche();
fputc(c,fp);
}
fclose(fp);
printf(\n Contents read :);fp=fopen(data.txt,r);
while(!feof(fp))
printf(%c,getc(fp));
}
OUTPUT :
Write data & to stop press .:
ABCDEFGHIJK.
Contents read: ABCDEFGHIJK.
8/10/2019 Chapter 14 Files .pptx
9/20Munindra Lunagaria
Text Modes
w (write)Opens a new file for writing
r (read)If file is found, loaded to memory; if not, returns NULL
a (append)
Opens a pre-existing file for appending dataw+ (write + read)
If file is found, its contents are destroyed; if not, new file is created
a+ (append + read)
Contents of file can be read and records can be added at the end offile
r+ (read + write)Read and write the record in file
Write a Program to Open a Pre existing Fi e an A
8/10/2019 Chapter 14 Files .pptx
10/20Munindra Lunagaria
Write a Program to Open a Pre-existing Fi e an A
Information at the End of File. Display the Contents of
the File Before and After Appending.
# include
# include
# include void main()
{
FILE *fp;
char c;
clrscr();printf(Contents of fi le before appending :\n);
fp=fopen(data.txt,r);
while(!feof(fp))
{
c=fgetc(fp);
printf(%c,c);
}
fp=fopen(data.txt,a);
8/10/2019 Chapter 14 Files .pptx
11/20Munindra Lunagaria
Continueif(fp==NULL)
{
printf(File can not appended);
exit(1);
}
printf(\n Enter string to append :);
while(c!=.)
{
c=getche();
fputc(c,fp);}
fclose(fp);
printf(\n Contents of file After
appending :\n);
fp=fopen(data.txt,r);
while(!feof(fp)){
c=fgetc(fp);
printf(%c,c);
}
}
OUTPUT :
Contents of file before appending:
String is terminated with \0.
Enter string to append :
This character is called as NULL
character.
Contents of file After appending:
String is terminated with \0.
This character is called as NULL
character
8/10/2019 Chapter 14 Files .pptx
12/20Munindra Lunagaria
wb (write)Opens a binary file in write mode
rb (read)Opens a binary file in read mode
ab (append)Opens a binary file in append mode
r+b (read + write)Opens a pre-existing file in read and write mode
w+b (read + write)Creates a new file in read and write mode
a+b (append + write)Opens a file in append mode. If file does not exist, new file is created
Binary Modes
P t U + M d f W iti d R di f
8/10/2019 Chapter 14 Files .pptx
13/20Munindra Lunagaria
Program to Use w+ Mode for Writing and Reading of a
File
# include
# include
# include void main()
{
FILE *fp;
char c= ;
clrscr();
fp=fopen(data.txt,w+);if(fp==NULL)
{
printf(Can not open file);
exit(1);
}
}
8/10/2019 Chapter 14 Files .pptx
14/20Munindra Lunagaria
printf(Write data & to stop press . :);
while(c!= .)
{
c=getche();
fputc(c,fp);
}rewind(fp);
printf(\n Contents read :);
while(!feof(fp))
printf(%c,getc
(fp));}
OUTPUT :
Write data & to stop press .: ABCDEFGHIJK.
Contents read : ABCDEFGHIJK.
Continue
8/10/2019 Chapter 14 Files .pptx
15/20Munindra Lunagaria
fprintf(): writes contents to the file
fscanf(): reads contents from the file pointed by file pointer
getc(): reads a single character from opened file and moves the file pointer
putc(): writes a single character into a file
fgetc(): reads a character and increments pointer
fputc(): writes the character to file
fgets(): reads string from a file
fputs(): writes string to a file
putw(): writes an integer value to the file
getw(): returns the integer value from a file
FileI/O
8/10/2019 Chapter 14 Files .pptx
16/20Munindra Lunagaria
# include
# include
void main(){
FILE *fp;
char text[15]; OUTPUT:
int age; Name AGE
fp=fopen(Text.txt,w+); AMIT 12
clrscr(); Name AGE
printf(NametAGE\n); AMIT 12
printf(Name\t AGE\n );
scanf(%s %d,text,&age);
fprintf(fp,%s %d, text,age);
printf(Name\t AGE\n );fscanf(fp,%s %d,text,&age);
printf(%s\t%d\n, text,age);
fclose(fp);
}
A Simple Program to Demonstrate the Use of fscanf() and fprintf().
8/10/2019 Chapter 14 Files .pptx
17/20Munindra Lunagaria
fseek(): This function is used to seek the file pointer at specified
position.
fseek(filepointer,offset,position);
Position can be
SEEK_SETSEEK_CUR
SEEK_END
ftell(): This function is used to tell the current position of file
pointer.
P=ftell(filepointer);
Other File Operation
W it P t R d th L t F Ch t
8/10/2019 Chapter 14 Files .pptx
18/20Munindra Lunagaria
# include
# include
# include
void main()
{
FILE *fp;
int n,ch;
clrscr();
fp=fopen(text.txt,r);
printf(\nContents of fi le\n);
while((ch=fgetc(fp))!=EOF)
printf(%c,ch);
printf(\n Howr many characters including spaces would you like
to skip ? :);
scanf(%d,&n);
Write a Program to Read the Last Few Charactersof the Fiile Using the fseek()Statement
8/10/2019 Chapter 14 Files .pptx
19/20Munindra Lunagaria
fseek(fp,-n-2,SEEK_END);
printf(\nLast %d characters of a fi le\n,-n-2);
while((ch=fgetc(fp))!=EOF)
printf(%c,ch);
fclose(fp);
}
OUTPUT:
How many characters including spaces would you like to
skip? : 4
Last 5 characters of a file
WORLD
Continue
8/10/2019 Chapter 14 Files .pptx
20/20
Munindra Lunagaria