Chapter 8 : File Operation
Chapter 8
File Operation
Chapter 8 : File Operation
Objectives
Understand the files input and output operation Capable to use and manipulate files operation
Chapter 8 : File Operation
Files
In C, file is used to store information permanently on a hard disk. The data in file still hold the information even the computer is off. C provides a set of library function for creating and processing information stored in data files. Files created can be read by any text mode editor such as notepad.
Chapter 8 : File Operation
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 any operation can be perform.
Chapter 8 : File Operation
Opening Files
Before read information from or write information to file, an area of memory called buffer must be created. Buffer store information temporarily when reading from hard disk or writing information from memory to hard disk. Buffer is created using: FILE *input
Chapter 8 : File Operation
File Declaration
Use stdio.h library Format :
FILE *input;
FILE will create buffer area.
input is a file pointer that point to the location of data in buffer area.
Chapter 8 : File Operation
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);
Chapter 8 : File Operation
Access
Mode
r w a r+ w+ a+
Decription
Opens file for reading Opens a new file for writing (Create) Opens file for appending (adding to it) Opens file for update (read & write) Open a new file for reading and writing Opens file for update (read the entire file, or write to the end of it)
Chapter 8 : File Operation
Outline Program for Opening & Closing File
#include <stdio.h> FILE *input; input = fopen([Link],w); . . fclose(input);
Chapter 8 : File Operation
Example Create & Write to file
#include <stdio.h> main() { FILE *input; /*declare a file */ input = fopen([Link], w); /*create the file*/ fprintf(input, Welcome to the world!); /* write to the file*/ fclose(input);/*close the file*/ }
10
Chapter 8 : File Operation
Writing to a File
Must open in write, add or update access mode To write to a file, use fprintf() function.
11
The statement: fprintf(input, Welcome to the world!); writes the output to file pointed by input. Note that the file pointer input appears as the first argument in the argument list.
Chapter 8 : File Operation
Exercise
Create a file name [Link] and write the following data using for control statement. 0123456789
12
Chapter 8 : File Operation
Reading from a File
13
To read data from file, it must be opened in read or update access mode.
fopen([Link], r);
To read from a file, use fscanf() function. Check the end-of-file by using the EOF.
Chapter 8 : File Operation
Example Read a file
#include <stdio.h> main() { FILE *input; /*declare a file */ input = fopen([Link], 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*/ }
14
Chapter 8 : File Operation
FEOF
if (!feof(input)) { fscanf(input,"%d",&num); }
15
Chapter 8 : File Operation
Exercise
Read a file name [Link] And display its content on screen
16
Chapter 8 : File Operation
File Open Verification
Under certain circumstances, an attempt to open a file may fail file may not exist. In this case fopen function is fail and the program cannot execute properly. Good programming practice to verify the success of fopen and terminate the execution in case of failure. If fopen function call returns a NULL, then the call is failed.
17
Chapter 8 : File Operation
File Open Verification
18
A program execution may terminate by using the exit function declared in stdlib.h header file. The exit function has one integer argument, which is returned to the operating system upon program termination: exit (-1) In common practice a negative value is taken as indication of abnormal program termination.
Chapter 8 : File Operation
Example
#include <stdio.h> #include <conio.h> #include <stdlib.h> 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); }
19
Chapter 8 : File Operation
Exercise
Write a function name open_file that read a file name [Link] and display its content on screen. Put the file verification in your function. The main program 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(); }
20