Chapter 8 File Operation
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
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.
Files
file
Writing
First, the file must be open before any operation can be perform.
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
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.
Use fopen() function to open files Format : FILE *input; input = fopen(filename,access); Use fclose() function to close it Format : fclose(input);
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)
10
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.
Exercise
Create a file name result.txt and write the following data using for control statement. 0123456789
12
13
To read data from file, it must be opened in 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.
14
FEOF
if (!feof(input)) { fscanf(input,"%d",&num); }
15
Exercise
Read a file name result.txt And display its content on screen
16
17
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.
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
Exercise
Write a function name open_file that read a file name result.txt 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