Download - Chapter11_FilesProcessing
-
7/29/2019 Chapter11_FilesProcessing
1/15
Chapter 11: Data Files & File Processing
In this chapter, you will learn aboutFiles and streams
Creating a sequential access file
Reading data from a sequential access fileUsing fgetc() and fputc()
Using fgets() and fputs()
Using fprintf() and fscanf()
Using fopen() and fclose()
-
7/29/2019 Chapter11_FilesProcessing
2/15
Files and Streams
C views a file as a sequential stream ofbytes.
A file ends either with an EOF (end-of-file)marker or at a specified byte number
specified by the system.When a file is opened, a stream is associatedwith a file.
Streams provide communication channels
between files and the programs.
0 1 2 3 4 5 6 7 8 . . . . . n-1
-
7/29/2019 Chapter11_FilesProcessing
3/15
Files and Streams
In addition to providing access to a file, astream can also be used to access devices.For example, when a program (any program)is executed, 3 streams are automaticallyopened:
standard input (stdin)enable the program to read data from keyboard
standard output (stdout)
enable the program to print data on the screen
standard error (stderr)enable program to print errors on the screen
They are all manipulated using file pointers.
-
7/29/2019 Chapter11_FilesProcessing
4/15
Declaring a file
The standard library provides someof the file manipulation function.
Declaring file:
FILE *the_file;States that thefile is a pointer to a file structure
If there is more than one file, each file needs
to have its own FILE pointer.
thefile is an internal file name used by theprogram to refer to the external file name (the
actual physical file stored on disk).
-
7/29/2019 Chapter11_FilesProcessing
5/15
File operation: fopen ()
Before we can process a file, we must eitheropen or create it.
Syntax for fopen( ):
internal_file_name = fopen(external_file_name, OpenMode);
The fopen() function takes 2 arguments: the filename and the file mode.
-
7/29/2019 Chapter11_FilesProcessing
6/15
File operation: fopen ()
Example:
thefile = fopen(my_file.txt,r);
This statement will try to open a file called
my_file.txt.my_file.txt is the external file name referring to aphisycal file on the disk.
If the file is to be placed in a different directorythan the program directory, the full path need to
be specified. For example: D:/my_file.txtThe function returns a pointer to the successfullyopened file. But if the file cannot be opened, aNULL is returned.
-
7/29/2019 Chapter11_FilesProcessing
7/15
File OpenMode:
The following is the list of open mode that can be associated withfopen( ).
r : to open existing file in input mode so data can be read from
it. The file to be opened must exist phisically on disk for this
open mode to be successful.
w : the file is intended to be an output file and we are planningto write data to it.
a : to append data to the end of existing file.
r+ : to open existing file for update (both in input and output
mode). If the file already exist, the content is not destroy.
w+ : destroy the file if already exist and open a new file for
update.
a+ : open file foe update (adding data at the end of the file.
-
7/29/2019 Chapter11_FilesProcessing
8/15
fopen( )
You could also ask the users to specify thename of the file s/he want to open.
Example:
char filename[50];
printf("Enter file name: ");
gets(filename);
thefile = fopen(filename,"r");
The users must enter the full filename,including the extension (.txt, .doc etc) andpath.
-
7/29/2019 Chapter11_FilesProcessing
9/15
File operation: fail fopen( )
If fopen( ) is returning a NULL value, thismeans that the fopen( ) has failed.
This is due to any of the following reasons:
Opening a non-existing file for reading
Opening a file for reading or writing without
having granted the appropriate access to the
file by the operating system.
Opening a file for writing when no disk space is
available.Therefore, in our program, we need to writestatements that would handle this failure.
-
7/29/2019 Chapter11_FilesProcessing
10/15
fgetc( )
fgetc( ) is a function that would read acharacter from a file.
Syntax:
variable = fgetc(internal_file_name);
Where:variable is a character variable to hold the
value returned by fgetc( ).
Internal_file_name is the pointer to an openfile.
-
7/29/2019 Chapter11_FilesProcessing
11/15
fgetc( ): Example
#include void main(void){
char ch;FILE *thefile;if ((thefile = fopen(my_file.txt","r")) == NULL){
printf("File fail to open\n");}else{
/* read a character from the file */ch = fgetc(thefile);
/* print the value of ch to the screen */printf("%c",ch); }
}fclose(thefile);
}
-
7/29/2019 Chapter11_FilesProcessing
12/15
fputc( )
fputc( ) is a function that would write acharacter value to the file.
Syntax:
Using character variable, ch:
fputc(ch,internal_file_name);
Using character constant:
fputc(a,internal_file_name);
-
7/29/2019 Chapter11_FilesProcessing
13/15
fscanf( )
fscanf( ) is a function that read a value, or values ofmix data type from a file.
Syntax:
fscanf(internal_name,formatControl, variableList);
Example:
char name[50];
int age;fscanf(theFile, %s %d, name, &age);
-
7/29/2019 Chapter11_FilesProcessing
14/15
fprintf( )
fprintf( ) is a function that would print values to thefile
Syntax:fprintf(internal_name, variable expression);
Example 1:fprintf(theFile, This is an example);
Example 2:char name[50] = Ahmad
fprintf(theFile, name);
Example 3:char name[50] = Ahmad;fprintf(theFile, My name is name);
-
7/29/2019 Chapter11_FilesProcessing
15/15
fgets( )
fgets( ) read a string of characters from a file.
Syntax:fgets(stringVariable, size, internal_name);
Example:char content[50];
fgets(content, 50, theFile);
fgets( ) will read size
1 character from thefile pointed by theFile, and saves the stringinto variable content.