File Processing
Part 2: Binary File Processing
/* Read Chapter 4 of the Course Notes for details. */
サルバド-ル・フロランテ 1
Q: What are the basic file processing
steps?
1. Declare a file pointer variable
2. Open the file (either for read, write, or both)
3. Read or write (or both) data from/to the file
4. Close the file
2
Quick example: binary file processing (write)
#include <stdio.h>
int
main()
{ int data = 1;
// 1. Declare a file pointer variable
FILE *fp;
// 2. Open a file for write (output)
fp = fopen(“sample.dat”, “wb”);
// 3. Write data to the file
fwrite(&data, sizeof(int), 1, fp);
// 4. Close the file
fclose(fp);
return 0;
}
3
Quick example: binary file processing (write)
#include <stdio.h> Q1: How do you open a binary file for write/output?
int Q2: How do you write data to a binary file?
main() Q3: Open stdio.h. What is the function prototype
of fwrite()
{ int data = 1;
Q4: What is the meaning of each fwrite() param?
Q5: What is the size of the file sample.dat?
FILE *fp;
fp = fopen(“sample.dat”, “wb”);
fwrite(&data, sizeof(int), 1, fp);
fclose(fp);
return 0;
}
4
Exercise: Complete the following program.
#include <stdio.h>
int main()
{
FILE *fp;
char ch = ‘A’;
int i = 123;
float f = 6.78f;
double d = 3.1416;
fp = fopen(“binary.dat”, ____); // write mode
// write values of ch, i, f, & d to the file
fwrite(___, ____, ____, ____);
fwrite(___, ____, ____, ____);
fwrite(___, ____, ____, ____);
fwrite(___, ____, ____, ____);
fclose(fp);
return 0;
} 5
#include <stdio.h> File Position Value
int 0 A ch
main()
{ 1 i
FILE *fp;
char ch = ‘A’; 123
int i = 123;
float f = 6.78f;
double d = 3.1416; 5 f
fp = fopen(“binary.dat”, “wb”); 6.78f
fwrite(&ch, sizeof(char), 1, fp);
fwrite(&i, sizeof(int), 1, fp);
fwrite(&f, sizeof(float), 1, fp); 9 d
fwrite(&d, sizeof(double), 1, fp);
fclose(fp);
return 0; 3.1416
}
17 EOF 6
Quick example: binary file processing (read)
#include <stdio.h>
int
main()
{ int x;
// 1. Declare a file pointer variable
FILE *fp;
// 2. Open a file for read (input)
fp = fopen(“sample.dat”, “rb”);
// 3. Read data from the file
fread(&x, sizeof(int), 1, fp);
// 4. Close the file
fclose(fp);
return 0;
}
7
Quick example: binary file processing (read)
#include <stdio.h> Q1: How do you open a binary file for read/input?
int Q2: How do you read data to a binary file?
main() Q3: Open stdio.h. What is the function prototype
of fread()?
{ int x;
Q4: What is the meaning of each fread() param?
Q5: What is the value of x?
FILE *fp;
fp = fopen(“sample.dat”, “rb”);
fread(&x, sizeof(int), 1, fp);
fclose(fp);
return 0;
}
8
Exercise: Complete the following program.
#include <stdio.h>
int main()
{
FILE *fp;
char ch;
int i;
float f;
double d;
fp = fopen(“binary.dat”, ____); // read mode
// read values to ch, i, f, & d from the file
fread(___, ____, ____, ____);
fread(___, ____, ____, ____);
fread(___, ____, ____, ____);
fread(___, ____, ____, ____);
fclose(fp);
return 0;
} 9
Example: Writing Array elements
#include <stdio.h>
int main() Q1: How many times was fwrite() called?
{ Q2: What is the size of the file?
FILE *fp;
int A[5] = {10, 20, 30, 40, 50};
int i;
fp = fopen(“array.dat”, “wb”); // write
for (i = 0; i < 5; i++)
fwrite(&A[i], sizeof(int), 1, fp);
fclose(fp);
return 0;
}
10
Example: BETTER way of writing array elements
#include <stdio.h>
int main() Q1: How many times was fwrite() called?
{ Q2: What is the size of the file?
FILE *fp;
int A[5] = {10, 20, 30, 40, 50};
int i;
fp = fopen(“array.dat”, “wb”); // write
fwrite(A, sizeof(int), 5, fp);
fclose(fp);
return 0;
}
11
Example: Reading Array elements
#include <stdio.h>
int main() Q1: How many times was fread() called?
{ Q2: What is the size of the file?
FILE *fp;
int B[5];
int i;
fp = fopen(“array.dat”, “rb”); // read
for (i = 0; i < 5; i++)
fread(&B[i], sizeof(int), 1, fp);
fclose(fp);
return 0;
}
12
Example: BETTER way of reading array elements
#include <stdio.h>
int main() Q1: How many times was fwrite() called?
{ Q2: What is the size of the file?
FILE *fp;
int B[5];
fp = fopen(“array.dat”, “rb”); // read
fread(B, sizeof(int), 5, fp);
fclose(fp);
return 0;
}
13
Exercise: Writing/Reading one structure
// sample structure
struct nameTag {
char First[20];
char Last[20];
};
struct studentTag {
int ID;
struct nameTag name;
float grade;
};
typedef struct studentTag Student;
// assume x was declared as Student x;
// assume fp was declared as FILE *fp
Q1: How do you read the contents of x from a file?
fread(___, _________________, _____, ____);
Q2: How do you write the contents of x to a file?
fwrite(___, _________________, _____, ____);
14
Exercise: Writing/Reading an Array of Structures
// sample structure
struct nameTag {
char First[20];
char Last[20];
};
struct studentTag {
int ID;
struct nameTag name;
float grade;
};
typedef struct studentTag Student;
// assume S was declared as Student S[100];
// assume fp was declared as FILE *fp
Q1: How do you read the contents of S from a file?
fread(___, _________________, _____, ____);
Q2: How do you write the contents of S to a file?
fwrite(___, _________________, _____, ____);
15
Q: How do perform random access on
binary files?
• Random access – to access an element directly without going
through other elements
• Related functions:
– long int ftell(FILE *);
– int fseek(FILE *, long offset, int origin);
Options for origin:
SEEK_SET beginning of the file
SEEK_END end of the file
SEEK_CUR current file position
16
Quick example: ftell()
#include <stdio.h> Q1: What value is printed in the 1st printf()?
int Q2: What value is printed in the 2nd printf()?
main()
{ int x;
long int fpos;
FILE *fp;
fp = fopen(“sample.dat”, “rb”);
fpos = ftell(fp);
printf(“fpos = %ld\n”, fpos);
fread(&x, sizeof(int), 1, fp);
fpos = ftell(fp);
printf(“fpos = %ld\n”, fpos);
fclose(fp);
return 0;
}
17
#include <stdio.h> File Position Value
int 0 A ch
main()
{ 1 i
FILE *fp;
char ch = ‘A’; 123
int i = 123;
float f = 6.78f;
double d = 3.1416; 5 f
fp = fopen(“binary.dat”, “wb”); 6.78f
fwrite(&ch, sizeof(char), 1, fp);
fwrite(&i, sizeof(int), 1, fp);
fwrite(&f, sizeof(float), 1, fp); 9 d
fwrite(&d, sizeof(double), 1, fp);
fclose(fp);
return 0; 3.1416
}
17 EOF 18
Quick example: fseek() with SEEK_SET
#include <stdio.h> File Position Value
0 A ch
int
Q1: What is the file position before fseek()? 1 i
main() Q2: What is the file position right after fseek()?
123
{
float f;
5 f
FILE *fp;
6.78f
fp = fopen(“binary.dat”, “rb”);
9 d
fseek(fp, 5, SEEK_SET);
fread(&f, sizeof(float), 1, fp); 3.1416
17 EOF
}
19
Quick example: fseek() with SEEK_END
#include <stdio.h> File Position Value
int 0 A ch
Q1: What is the file position before fseek()? 1 i
main()
Q2: What is the file position right after fseek()? 123
{
float f;
FILE *fp; 5 f
6.78f
fp = fopen(“binary.dat”, “rb”);
9 d
fseek(fp, -12, SEEK_END);
fread(&f, sizeof(float), 1, fp);
3.1416
17 EOF
}
20
Quick example: fseek() with SEEK_CUR
#include <stdio.h> File Position Value
0 A ch
int Q1: What is the file position before fseek()? 1 i
main() Q2: What is the file position right after fseek()?
123
{ char ch;
float f;
5 f
FILE *fp;
6.78f
fp = fopen(“binary.dat”, “rb”);
9 d
fread(&ch, sizeof(char), 1, fp);
fseek(fp, 4, SEEK_CUR);
fread(&f, sizeof(float), 1, fp); 3.1416
17 EOF
}
21
Exercise: What are the correct fseek() parameters for the ff:?
Q1: Set file position for reading the double data File Position Value
0 A ch
value. Use SEEK_SET as origin. 1 i
123
Q2: Set file position for reading the double data
value. Use SEEK_END as origin.
5 f
Q3: Set file position for reading the integer value. 6.78f
Use SEEK_SET as origin.
9 d
Q4: Set file position for reading the integer value.
Use SEEK_END as origin.
3.1416
Q5: The integer value was just read from the file.
Set file position for reading the double data value.
Use SEEK_CUR as origin. 17 EOF
22
Q: How do you perform mixed mode processing
(i.e., reading and writing on the same binary file)
• “wb” is write only
• “rb” is read only
• “w+b” (or “wb+”) allows both reading and
writing on the same file; if the file exists, its
original contents will be overwritten
• “rb+” (or “r+b”) allows both reading and
writing on the same file; it is assumed that
the file already exists
23
#include <stdio.h> File Position Value
int 0 A ch
main()
{ 1 i
FILE *fp;
char ch = ‘A’; 123
int i = 123;
float f = 6.78f;
double d = 3.1416; 5 f
fp = fopen(“binary.dat”, “wb”); 6.78f
fwrite(&ch, sizeof(char), 1, fp);
fwrite(&i, sizeof(int), 1, fp);
fwrite(&f, sizeof(float), 1, fp); 9 d
fwrite(&d, sizeof(double), 1, fp);
fclose(fp);
return 0; 3.1416
}
17 EOF 24
Quick example: ”rb+” mode
#include <stdio.h>
int
main()
{ float x = 9.99f;
FILE *fp;
// Open a file in mixed mode “rb+”
fp = fopen(“binary.dat”, “rb+”);
// change file position
fseek(fp, sizeof(char) + sizeof(int), SEEK_SET);
// overwrite existing data
fwrite(&x, sizeof(float), 1, fp);
fclose(fp);
return 0;
}
25
Quick example: ”rb+” mode
#include <stdio.h> Q1: What will happen in case the file “binary.dat” does
int not actually exist?
main() Q2: What is the file position right after fopen()?
{ float x = 9.99; Q3: What is the file position right after fseek()?
Q4: What is the file position right after fwrite()?
FILE *fp;
// Open a file in mixed mode “rb+”
fp = fopen(“binary.dat”, “rb+”);
// change file position
fseek(fp, sizeof(char) + sizeof(int), SEEK_SET);
// overwrite existing data
fwrite(&x, sizeof(float), 1, fp);
Q5: In which file position was the value of x written?
fclose(fp);
Q6: What are the contents of the binary file after
return 0;
executing the program?
}
26
Sample programs
Refer to the following programs:
• writer.c // creates a binary file, mode “wb”
• reader.c // reads a binary file, mode “wb”
• rbplus1.c // mixed mode “rb+”
• rbplus2.c // mixed mode “rb+”
• rbplus3.c // mixed mode “rb+”
27
28
29
Q1: What is the current file position before fwrite()?
Q2: Which file position was modified?
Q3: What is the current file position after fwrite()?
Q4: Can you describe what happened to
binary.dat after executing the program?
30
31
Q1: What is the current file position after the 1st
fseek()?
Q2: Which is the file position after fread()?
Q3: What is the current file position after the 2nd
fseek()?
Q4: Can you describe what happened to
binary.dat after executing the program?
Q5: Which file positions were modified?
Q6: If the program is executed again, what is the
effect on the binary.dat?
32
33
Q1: Which file positions will be modified?
Q3: What will be the contents of binary.dat
if the program is executed three times
in a row?
34
Q: How do you check for the end of
file?
• returns 0 if the end of file is reached;
• otherwise it returns a non-zero value
• can be used for both text and binary files
Open bbtest.c, and understand the role of
feof() in the program.
Q: How do you flush data onto a file?
• forces writing of data onto file
• can be used for both text and binary files
• returns 0 if operation is successful
Example:
Example:
Exercise
• Write a program that will replace the double data
value in “binary.dat” file with 1.234567.
• Write a program that will read the contents of
“array.dat” one integer at a time (recall that the file
contains 10 20 30 40 and 50). Thereafter, each value
in the binary file should be replaced with a value
that is twice the original. For example, if the original
value in the file is 10, then it will be replaced by 20.
(Thus, the new file should contain 20 40 60 80 and
100.)
41
42
43
44
-- The End --
サルバド-ル・フロランテ 45