Chapter11_FileProcessing
Transcript of Chapter11_FileProcessing
7/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 1/22
CSEB114: Principle of programming
Chapter 11: Data Files & File Processing
7/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 2/22
Objectives
prepared by NI, edited by MAF
In this chapter, you will learn about Files and streams
Creating a sequential access file
Reading data from a sequential access file
Using fgetc() and fputc() Using fgets() and fputs()
Using fprintf() and fscanf()
Using fopen() and fclose()
7/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 3/22
Files and Streams
prepared by NI, edited by MAF
C views a file as a sequential stream of bytes. 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 associated with a
file. Streams provide communication channels between
files and the programs.
0 1 2 3 4 5 6 7 8 . . . . . n-1
7/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 4/22
Files and Streams
prepared by NI, edited by MAF
In addition to providing access to a file, a stream canalso be used to access devices.
For example, when a program (any program) isexecuted, 3 streams are automatically opened: 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/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 5/22
Declaring a file
prepared by NI, edited by MAF
The standard library <stdio.h> provides some of thefile 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 the program to
refer to the external file name (the actual physical file
stored on disk).
7/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 6/22
File operation: fopen ()
prepared by NI, edited by MAF
Before we can process a file, we must either open orcreate 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/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 7/22
File operation: fopen ()
prepared by NI, edited by MAF
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 a
phisycal file on the disk. If the file is to be placed in a different directory than the
program directory, the full path need to be specified.For example: “D:/my_file.txt”
The function returns a pointer to the successfullyopened file. But if the file cannot be opened, a NULL isreturned.
7/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 8/22
File OpenMode:
prepared by NI, edited by MAF
The following is the list of open mode that can be associated with
fopen( ).
“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 for update (adding data at the end of the file.
7/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 9/22
fopen( )
prepared by NI, edited by MAF
You could also ask the users to specify the name ofthe 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) and path.
7/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 10/22
File operation: fail fopen( )
prepared by NI, edited by MAF
If fopen( ) is returning a NULL value, this means thatthe 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 theappropriate 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 write
statements that would handle this failure.
7/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 11/22
fputc( )
prepared by NI, edited by MAF
fputc( ) is a function that would write a charactervalue to the file.
Syntax: Using character variable, ch:
fputc(ch, internal_file_name); Example:
char m = „c‟;
fputc(m, theFile);
Using character constant:
fputc(„a‟,internal_file_name);
Example
fputc(„c‟, theFile);
7/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 12/22
fputc() example
prepared by NI, edited by MAF
#include <stdio.h>
int main(void){
char ch;
FILE *thefile;
if ((thefile = fopen("file_putc.txt","w")) == NULL)
{
printf("File fail to open\n");
}
else
{
printf("Enter a character: ");
scanf("%c", &ch);
fputc(ch, thefile);}
fclose(thefile);
return 0;
}
7/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 13/22
fgetc( )
prepared by NI, edited by MAF
fgetc( ) is a function that would read a character froma 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 open file.
Example:
char c;
fgetc(theFile);
7/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 14/22
fgetc( ): Example
prepared by NI, edited by MAF
#include <stdio.h>
int main(void){
char ch;
FILE *thefile;
if ((thefile = fopen("file_getc.txt","r")) == NULL)
{
printf("File fail to open\n");
}
else
{
ch = fgetc(thefile); /*read a character from the file */
printf("%c\n",ch);/* print the value of ch to the screen
*/}
fclose(thefile);
return 0;
}
7/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 15/22
fprintf( )
prepared by NI, edited by MAF
fprintf( ) is a function that would print values to the file
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/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 16/22
fprintf( ): Example
prepared by NI, edited by MAF
#include<stdio.h>
int main(){
int i, power;
FILE *thefile;
if((thefile = fopen("file_fprintf.txt","w"))==NULL)
printf("File could not be open\n");
else{
for(i=1;i<=100;i++){
power=i*i;
fprintf(thefile,"%d\n",power);
}
printf("End of operations");
}fclose(thefile);
return 0;
}
7/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 17/22
fscanf( )
prepared by NI, edited by MAF
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/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 18/22
fscanf( ): Example
prepared by NI, edited by MAF
#include<stdio.h>
void main(){
int i, bil, matrik;
char grade[2];
char name[10];
FILE *thefile;
if((thefile = fopen("file_fscanf.txt","r")) == NULL)
printf("File could not be open");
else{
fscanf(thefile,"%d",&bil);
printf("%-10s%-10s%s\n","Matrik","Name","Grade");
for(i=0;i<bil;i++)
{fscanf(thefile,"%d%s%s",&matrik,name,&grade);
printf("%-10d%-10s%s\n",matrik,name,grade);
}
fclose(thefile);
}
}
7/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 19/22
fputs()
prepared by NI, edited by MAF
Writes the string to the file. The function begins outputting the string until it
reaches the terminating null character ('\0').
Syntax
fputs (string variable, internal name);
Example:
char str [20] = “gummy bear”;
fputs (str, theFile);
7/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 20/22
fputs( ): Example
prepared by NI, edited by MAF
#include<stdio.h>
int main(){
char str[20]= "let's picnic!";
FILE *thefile;
if((thefile = fopen("file_fputs.txt","w"))==NULL)
printf("File could not be open\n");
else
fputs(str, thefile);
fclose(thefile);
return 0;
}
7/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 21/22
fgets( )
prepared by NI, edited by MAF
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 readsize – 1
character from the filepointed by theFile, and saves the string into variable
content .
7/27/2019 Chapter11_FileProcessing
http://slidepdf.com/reader/full/chapter11fileprocessing 22/22
fgets( ): Example
prepared by NI, edited by MAF
#include<stdio.h>
int main(){
char str[30]= {'\0'};
FILE *thefile;
if((thefile = fopen("file_fgets.txt","r"))==NULL)
printf("File could not be open\n");
else{
fgets(str, 30, thefile);
puts(str);
}
fclose(thefile);
return 0;
}