LECTURE 22-23: Text and Binary File Processing (Chapter 12)
File Pointer Variables
//
To declare a file pointers for input and output text
files
FILE *infilep;
FILE *outfilep
//
To initialize infilep
to point to file b:date.txt that is
read-only
infilep = fopen(“b:data.txt”,
“r”);
//
Check if file was opened successfully
if (infilep == NULL)
printf(“Cannot
open the file \n”)
//
To initialize outfilep
outfilep = fopen(“b:results.txt”,”w”);
Text File Processing
The
opened files can be read from and manipulated using the following functions
that can take file pointer arguments:
fscanf, fprintf, getc, putc
These
functions are analogous to
scanf, printf, getchar, putchar
Read
TABLE 12.4 to understand the differences between these two groups of functions!
//
To close both files
fclose(infilep);
fclose(outfilep);
Useful
example (FIGURE 12.1) : Program to Make a Backup
Copy of a Text File
/*
* Makes a backup
file. Repeatedly prompts for the name of
a file to
* back up until a
name is provided that corresponds to an available
* file. Then it prompts for the name of the backup
file and creates
* the file copy.
*/
#include <stdio.h>
#define STRSIZ 80
int main(void)
{
char in_name[STRSIZ], /*
strings giving names */
out_name[STRSIZ]; /* of
input and backup files */
FILE *inp, /*
file pointers for input and */
*outp; /*
backup files */
char ch; /*
one character of input file */
/* Get
the name of the file to back up and open the file for input */
printf("Enter
name of file you want to back up> ");
for (scanf("%s", in_name);
(inp = fopen(in_name, "r")) == NULL;
scanf("%s", in_name)) {
printf("Cannot
open %s for input\n", in_name);
printf("Re-enter
file name> ");
}
/* Get name to use for
backup file and open file for output */
printf("Enter
name for backup copy> ");
for (scanf("%s",
out_name);
(outp = fopen(out_name, "w")) == NULL;
scanf("%s",
out_name)) {
printf("Cannot
open %s for output\n", out_name);
printf("Re-enter
file name> ");
}
/* Make backup copy
one character at a time */
for (ch = getc(inp); ch != EOF; ch = getc(inp))
putc(ch, outp);
/* Close files and
notify user of backup completion */
fclose(inp);
fclose(outp);
printf("Copied
%s to %s.\n", in_name, out_name);
return(0);
}
Binary
File Processing
Text
files are useful for storing and processing text files. However, many data
files are of different nature – for example, image, video, audio files. In this
case, it is more efficient to use binary files.
Functions
fread and fwrite are used to read and write to binary files, and they are analogous to fprintf and fscanf that are used for text files.
Example
(FIGURE 12.3): Creating a Binary File
of Integers
FILE *binaryp;
int i;
binaryp = fopen("nums.bin", "wb");
/* “wb” means that file is binary write-only */
for (i = 2; i <= 500; i += 2)
fwrite(&i, sizeof (int),
1, binaryp);
fclose(binaryp);
Let
us consider arguments of fwrite
function:
-
&i is address of a
number to be stored
-
sizeof(int) informs computer about the number of bytes needed to
store an integer
-
1 means that only one integer is to be written
-
binaryp
is a pointer to file where information is to be written
If
we wanted to store to binary file an array of integers score declared as
int score[10];
we would
write
fwrite(score,
sizeof(int), 10, binaryp);
For
more detailed explanation about the differences between text and binary file
processing, check TABLE 12.5!!