Upload
jackharish
View
218
Download
0
Embed Size (px)
Citation preview
8/14/2019 C-Programming-Class 7
1/24
1
Session 07
File Handling in C
8/14/2019 C-Programming-Class 7
2/24
2
Session Objectives
To know how to create and handle files.
To be able to differentiate between various
types of files. To be in a position to write involved
programs like inventory control, record
handling in files etc.,
8/14/2019 C-Programming-Class 7
3/24
3
Session Topics
File the structured data type
Temporary and permanent file
Organization of file
File operation
8/14/2019 C-Programming-Class 7
4/24
4
Files
Files are helpful in providing permanent storage ofinput/output that can be accessed at any point oftime later.
A file is a region of memory space in the storagemedia and it can be accessed using the libraryfunctions available in stdio.h or by the systemcalls of the operating system.
File contains bytes of information.
NOTE:It is not a data type.
8/14/2019 C-Programming-Class 7
5/24
5
Classification of Files
The files which are
accessed by using library
functions are known as
High Level Files.
These files areportable as
they make use of library
functions common to all
operating systems.
Stream oriented files.
The files which areaccessed by using systemcalls of the operating
system are known asLowLevel Files.
These files make use ofthe system calls of theoperating system under
which the program is run. System oriented files.
High Level Files Low Level Files
8/14/2019 C-Programming-Class 7
6/24
6
Streams
A stream is a source of data or destination of data that may
be associated with a disk or other I/O devices.
The source stream provides data to a program and it is
known as an input stream.
The destination stream receives the output from the
program and is known as output stream.
A stream is a pointer to a buffer of memory which is used
for transferring the data.
8/14/2019 C-Programming-Class 7
7/24
7
Types of I/O Streams
It is a sequence of
lines of text.
A file created using a
text stream is called as
a Text File.
It is a sequence of
unprocessed bytes.
A file created using a
binary stream is called
as a Binary File.
Text Stream Binary Stream
8/14/2019 C-Programming-Class 7
8/24
8
FILE & File Pointer
FILE is a structure defined in stdio.h and also it ispredefined data type using typedef.
The pointer to a FILE data type is called as a FilePointer.
A file pointer points to the block of information ofthe stream that has just been opened.
8/14/2019 C-Programming-Class 7
9/24
9
Why File Handling?
It is a very difficult to input large volume of data
through terminals and apart from this, it is time
consuming to enter such large volume of datausing keyboard.
If the program is terminated for any of the
reason or computer is turned off, the entire input
data is lost.
8/14/2019 C-Programming-Class 7
10/24
10
File Operations
The various file operations that are
performed are:
Open a fileRead data from the file
Write data into the file
Closing a file
8/14/2019 C-Programming-Class 7
11/24
11
1. Opened for read, write and append
2. File pointer is at the end of file
appenda+
1. Opened for read and write and the file contents
are lost if file exists.
2. If file does not exist, the file is read write and
file pointer points to beginning of file.
read writew+
Opening for read and write and the file pointer points
to the beginning of file.
read writer+
1. If the file already exists, file pointer is set to
end of file in write mode.2. If the file does not exists, it is created for
writing and file pointer is at the beginning.
appendA
1. If the file already exists, the contents are lost.
2. If file does not exist, a file is created for
writing.
write onlyW
File pointer is set to beginning if fileread onlyR
DescriptionMeaningMode
File Operating Modes
8/14/2019 C-Programming-Class 7
12/24
12
The file should be opened before reading a file or before writing into a file.
Syntax:fp = fopen(char *filename,char *mode)
where
-fp is a file pointer of type FILE
-filename holds the name of the file to be opened.Thefilename should be avalid identifier.
- mode details
Return Values:
- file pointer if successful
- NULL if unsuccessful
Example:
FILE *fp1,*fp2;
fp1=fopen(file_1,r);
fp2=fopen(file_2,w)
fopen()
8/14/2019 C-Programming-Class 7
13/24
13
This function is used to close the opened file. A file should be closed
when all file operations are over.
This ensures that all buffers are flushed and all the links to the file
are broken.
Syntax:
int fclose(FILE *fp)fp is a file pointer
Return Values:- 0 if successful
- EOF on error Example:
fclose(fp1);
fclose(fp2);
fclose()
8/14/2019 C-Programming-Class 7
14/24
14
#include
#define NULL 0
void main()
{
FILE *fp;
fp = fopen(sample.dat,r+);
if(fp = NULL)
printf(\n Error!Cannot open the file);
else
{
..;
fclose(fp);
}
}
File Handling:An Example
8/14/2019 C-Programming-Class 7
15/24
15
Syntax:
fscanf(fp,control string,list);
- fp is a file pointer
- the variables specified in the listwill take values from thefile specifed by fp using the specifications provided in
control string.
Example:
fscanf(fp,%d %s %f,&id,name,&salary);This function returns the number of items that are
successfully read from the file identified by file pointerfp.
fscanf()
8/14/2019 C-Programming-Class 7
16/24
16
Syntax:
fprintf(fp,Control string,list);
- fp is a file pointer
- the values of the variables specified in the listwill bewritten into the file identified by fp using the
specifications provided in control string.
Example:
fprintf(fp,%d %s %f,id,name,salary);This function returns the number of items that are
successfully written into the file identified by file pointer
fp.
fprintf()
8/14/2019 C-Programming-Class 7
17/24
17
Error Handling
During I/O operations, the following errors may occur
2. Reading beyond end of file marker
3. Unable to open a file
4. Invalid file name
5. Attempting to write to a write-protected file
feof()- This function is used to check for end of file.
- This function accepts the file pointer as a parameter.
Return values:
- If end of file is detected, a non zero value is returned.
- If end of file is not detected, zero is returned.
Example: If fp points to EOF
if(feof(fp))
printf(End of file is reached\n);
8/14/2019 C-Programming-Class 7
18/24
18
This macro is used to get the status of file.
This function accepts the file pointer as a parameter.
Return values:
-If error is detected, a non zero value is returned.
- If error is not detected, zero is returned.
Example: If fp is a file pointer
if(ferror(fp)!= 0)
printf(Error in the file \n);
ferror()
8/14/2019 C-Programming-Class 7
19/24
19
Program which provides error handling in files
void main(){
FILE *fp;
int i;
char file_name[10];
char ch;
printf(Enter the file name\n);
scanf(%s,file_name);
fp = fopen(file_name,r);
if(fp == NULL){
printf(Not successful\n);
exit(0);}
for(i=0;i
8/14/2019 C-Programming-Class 7
20/24
20
File Control Functions
ftell()This function is used to obtain the current position of the file pointer.
Syntax:
long ftell(FILE *fp);
Return Values:
- Returns the current position of the file pointer, on success
- Returns EOF on error.
Example:
If this function returns 1000, it indicates 999 characters have been read
so far from the file and the file pointer fp is pointing to the 1000th
character in the file.
8/14/2019 C-Programming-Class 7
21/24
21
This function is used to reposition the file pointer i.e, it is used to move the
file pointer to the desired position within the file.Syntax:
int fseek(FILE *fp,long offset, int start_point);
- fp is a file pointer
- offset can take positive,negative or zero value and specify the number ofbytes to be moved from the location specified by start_point.
- start_point an take one of the values.
0 indicates beginning of file.
1 indicates from the current position.
2 indicates from the end of file.
Return Values:
If success, returns 0.
Unsuccessful, returns non zero value.
Example:
fseek(fp,0,0 ) file pointer moves to the beginning of file
fseek()
8/14/2019 C-Programming-Class 7
22/24
22
This function accepts file pointer as a parameter and resets the
position of the file pointer to the start of the file.
Syntax:
rewind(file pointer);Example:
If file pointer fp is in position 1000, by executing this
function, the file pointer fp is set to the very first position in
the file.
rewind()
8/14/2019 C-Programming-Class 7
23/24
23
Summary
Files are helpful in providing permanent storage ofinput/output that can be accessed at any point of timelater.
A stream is a source of data or destination of data that maybe associated with a disk or other I/O devices.
FILE is a structure defined in stdio.h and also it ispredefined data type using typedef.
The pointer to a FILE data type is called as a File Pointer.A file pointer points to the block of information of thestream that has just been opened.
8/14/2019 C-Programming-Class 7
24/24