Chapter 14 Files
Chapter 14 Files
Chapter 14 Files
Files
Computer Programming and Utilization
Introduction
A file is nothing but a collection of records and each line of the data is recorded in
the file.
Munindra Lunagaria
A stream is a file and using physical device like keyboard information is stored in
the file.
The FILE object contains all the information about a stream like current position,
pointer to any buffer, error and end of file (EOF).
Munindra Lunagaria
File Types
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.
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
Munindra Lunagaria
Munindra Lunagaria
Closing a File
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);
Munindra Lunagaria
Munindra Lunagaria
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.
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 data
w+ (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 of
file
r+ (read + write)
Read and write the record in file
Munindra Lunagaria
# include <stdio.h>
# include <conio.h>
# include <process.h>
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);
Munindra Lunagaria
Continue
if(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);
}
}
Munindra Lunagaria
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
Binary Modes
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
Munindra Lunagaria
Continue
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.
Munindra Lunagaria
File I/O
fprintf(): writes contents to the file
Munindra Lunagaria
Munindra Lunagaria
OUTPUT:
Name
AMIT
Name
AMIT
AGE
12
AGE
12
Munindra Lunagaria
Continue
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
Munindra Lunagaria
Munindra Lunagaria