Transcript
8/22/2019 Chapter 8 File Operation
1/20
Chapter 8 : File Operation
1
Chapter 8
File Operation
8/22/2019 Chapter 8 File Operation
2/20
Chapter 8 : File Operation
2
Objectives
Understand the files input and outputoperation
Capable to use and manipulate filesoperation
8/22/2019 Chapter 8 File Operation
3/20
Chapter 8 : File Operation
3
Files
In C, file is used to store informationpermanently on a hard disk.
The data in file still hold the informationeven the computer is off.
C provides a set of library function forcreating and processing information
stored in data files. Files created can be read by any text
mode editor such as notepad.
8/22/2019 Chapter 8 File Operation
4/20
Chapter 8 : File Operation
4
Files
There are four operations in files:
Open/Create file
Writing to file
Reading from file
Close file
First, the file must be open before anyoperation can be perform.
8/22/2019 Chapter 8 File Operation
5/20
Chapter 8 : File Operation
5
Opening Files
Before read information from or writeinformation to file, an area of memorycalled buffermust be created.
Bufferstore information temporarilywhen reading from hard disk or writinginformation from memory to hard disk.
Buffer is created using: FILE *input
8/22/2019 Chapter 8 File Operation
6/20
Chapter 8 : File Operation
6
File Declaration
Use stdio.h library
Format :
FILE *input;FILE will create buffer area.
input is a file pointer that point tothe location of data in buffer area.
8/22/2019 Chapter 8 File Operation
7/20
Chapter 8 : File Operation
7
Opening & Closing Function
Use fopen()function to open files
Format :
FILE *input;input = fopen(filename,access);
Use fclose()function to close it
Format :fclose(input);
8/22/2019 Chapter 8 File Operation
8/20
Chapter 8 : File Operation
8
Access
Opens file for update (read the entire
file, or write to the end of it)
a+
Open a new file for reading andwriting
w+
Opens file for update (read & write)r+
Opens file for appending (adding toit)
aOpens a new file for writing (Create)
w
Opens file for readingr
DecriptionMode
8/22/2019 Chapter 8 File Operation
9/20
Chapter 8 : File Operation
9Outline Program for Opening &Closing File
#include
FILE *input;
input = fopen(Test.txt,w);.
.
fclose(input);
8/22/2019 Chapter 8 File Operation
10/20
Chapter 8 : File Operation
10
Example Create & Write to file
#include
main()
{
FILE *input; /*declare a file*/
input = fopen(test.txt, w); /*create the file*/fprintf(input, Welcome to the world!); /* write to
the file*/
fclose(input);/*close the file*/
}
Ch 8 Fil O i
8/22/2019 Chapter 8 File Operation
11/20
Chapter 8 : File Operation
11
Writing to a File
Must open in write, add or update accessmode
To write to a file, use fprintf() function.
The statement:fprintf(input, Welcome to
the world!); writes the output to file
pointed by input.
Note that the file pointerinputappears as
the first argument in the argument list.
Ch t 8 Fil O ti
8/22/2019 Chapter 8 File Operation
12/20
Chapter 8 : File Operation
12
Exercise
Create a file name result.txt
and write the following datausing forcontrol statement.
0 1 2 3 4 5 6 7 8 9
Ch t 8 Fil O ti
8/22/2019 Chapter 8 File Operation
13/20
Chapter 8 : File Operation
13
Reading from a File
To read data from file, it must be openedin read or update access mode.
fopen(text.dat, r);
To read from a file, use fscanf()
function.
Check the end-of-file by using the EOF.
Chapter 8 : File Operation
8/22/2019 Chapter 8 File Operation
14/20
Chapter 8 : File Operation
14
Example Read a file
#include
main()
{
FILE *input; /*declare a file*/
input = fopen(test.txt, r); /*read the file*/char str[20];/*declare a string*/
fscanf(input, %s,str); /* write to
the file*/
printf(%s,str); /*display to the screen the data*/
fclose(input);/*close the file*/
}
Chapter 8 : File Operation
8/22/2019 Chapter 8 File Operation
15/20
Chapter 8 : File Operation
15
FEOF
if (!feof(input))
{
fscanf(input,"%d",&num);}
Chapter 8 : File Operation
8/22/2019 Chapter 8 File Operation
16/20
Chapter 8 : File Operation
16
Exercise
Read a file name result.txt
And display its content onscreen
Chapter 8 : File Operation
8/22/2019 Chapter 8 File Operation
17/20
Chapter 8 : File Operation
17
File Open Verification
Under certain circumstances, an attemptto open a file may fail file may not exist.
In this case fopenfunction is fail and the
program cannot execute properly.
Good programming practice to verify thesuccess offopenand terminate the
execution in case of failure. Iffopenfunction call returns a NULL, then
the call is failed.
Chapter 8 : File Operation
8/22/2019 Chapter 8 File Operation
18/20
Chapter 8 : File Operation
18
File Open Verification
A program execution may terminate by using theexitfunction declared in stdl ib.hheader file.
The exit function has one integer argument,
which is returned to the operating system uponprogram termination: ex it (-1)
In common practice a negative value is taken asindication of abnormal program termination.
Chapter 8 : File Operation
8/22/2019 Chapter 8 File Operation
19/20
Chapter 8 : File Operation
19Example#include
#include
#include
void main()
{
FILE *input; /*declare a file */
char file_name[15];
printf("\nEnter the file name to open: ");
gets(file_name);
if ((input=fopen(file_name, "r")) ==NULL ){
printf("Cannot open file name %s", file_name);
getch();
exit(-1);
}
Chapter 8 : File Operation
8/22/2019 Chapter 8 File Operation
20/20
Chapter 8 : File Operation
20ExerciseWrite a function name open_file that read a filename result.txt and display its content on screen.
Put the file verification in your function. The mainprogram is provided as below:
void main()
{
char file_name[15];void open_file(char *);
printf("\nEnter the file name to open: ");
gets(file_name);
open_file(file_name);
getch();
}
top related