0% found this document useful (0 votes)
15 views10 pages

File IO2

This document discusses character input/output functions in C programming. It covers reading and writing single characters to text streams, and provides examples of using these functions to copy and manipulate text files. Several code examples are given to demonstrate how to copy files, remove tabs, merge sorted files, and handle errors when working with files.

Uploaded by

Thien Bui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views10 pages

File IO2

This document discusses character input/output functions in C programming. It covers reading and writing single characters to text streams, and provides examples of using these functions to copy and manipulate text files. Several code examples are given to demonstrate how to copy files, remove tabs, merge sorted files, and handle errors when working with files.

Uploaded by

Thien Bui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

12/2/2021

Character Input/Output
Functions

Character input functions read one character at a time


from a text stream. Character output functions write
one character at the time to a text stream.

Topics discussed in this section:


Terminal Character I/O
Terminal and File Character I/O
Character Input/Output Examples

FIGURE 7-12 Character Input/Output Functions

1
12/2/2021

PROGRAM 7-7 Create Text File

PROGRAM 7-7 Create Text File

2
12/2/2021

PROGRAM 7-7 Create Text File

PROGRAM 7-8 Copy Text File

3
12/2/2021

PROGRAM 7-8 Copy Text File

PROGRAM 7-9 Count Characters and Lines

4
12/2/2021

PROGRAM 7-9 Count Characters and Lines

PROGRAM 7-10 Count Words

10

5
12/2/2021

PROGRAM 7-10 Count Words

11

Ví Dụ Sao Chép File


/* Problem: copy an input file to an output file */
/* Technique: loop, copying one char at a time until
EOF, files must already be open before this */
status = fscanf(infilep, “%c”, &ch);
while (status != EOF) {
fprintf(outfilep, “%c”, ch);
status = fscanf(infilep, “%c”, &ch);
}
printf(“File copied.\n”);
fclose(infilep);
fclose(outfilep);
Trang 12

6
12/2/2021

Ví Dụ Sao Chép File

/* Many C programmers use this


style */
...
while (fscanf(infilep, “%c”,
&ch) != EOF)
fprintf(outfilep, “%c”, ch);

printf(“File copied.\n”);
fclose(infilep);
fclose(outfilep);
Trang 13

Ví Dụ Truy Vấn Cơ Sở Dữ Liệu


#include <stdio.h>
int main(void) {
FILE *inp, *outp;
int age, j; Equivalent query in SQL
char name[20], ssn[9], ch; database language:
inp = fopen(“db_file”, “r” );
outp = fopen(“result_file”, “w”);
/* loop till the end-of-file */ SELECT NAME, SSN
while (fscanf(inp, “%c”, &name[0]) != EOF) { FROM DB_FILE
/* read name, ssn, age */ WHERE AGE > 20;
for (j = 1; j < 20; j++)
fscanf(inp, “%c”, &name[j]);
for (j = 0; j < 9; j++)
fscanf(inp, “%c”,&ssn[j]);
fscanf(inp, “%d”, &age);
/* read line feed character */
fscanf(inp, “%c”, &ch);
/* copy name, ssn to output if age > 20 */
if (age > 20) {
for (j = 0; j < 20; j++)
fprintf(outp, “%c”, name[j]);
for (j = 0; j < 9; j++)
fprintf(outp, “%c, ssn[j]);
fprintf(outp, ”\n”);
}
}
fclose(inp); fclose(outp);
return (0);
}
Trang 14

7
12/2/2021

Ví Dụ Mở Rộng Tab
#include <stdio.h>
int main(void) {
FILE *infilep, *outfilep; Input: a b \t c
char ch; d \t e f
int column = 0; Output: a b c
/* Open input and output files */ d ef
infilep = fopen(“prog.c”, “r”);
outfilep = fopen(“tabless-prog.c”, “w”);
/* process each input character */
while (fscanf(infilep, “%c”, &ch) != EOF){
if (ch == ‘\n’ || ch == ‘\r’) {
/* end of line: reset column counter */
column = 0;
fprintf(outfilep, “%c”, ch);
} else if (ch == ‘\t’) {
/* tab: output one or more spaces, */
/* to reach the next multiple of 8. */
do {
fprintf(outfilep, “%c”, ‘ ‘) ;
column++;
} while ((column % 8) != 0);
} else {
/* all others: count it, and copy it out */
column ++;
fprintf(outfilep, “%c”, ch);
}
}
fclose(infilep); fclose(outfilep);
return 0;
} Trang 15

Ví Dụ Nối Hai File Có Thứ Tự


#include <stdio.h>
#define MAXLINE 10000 /*ASSUMES no line longer*/
int main(void) {
FILE *in1p, * in2p, *outp;
char buffer1[MAXLINE], buffer2[MAXLINE];
char *stat1, *stat2;
in1p = fopen(“sorted-file1”, “r”);
in2p = fopen(“sorted-file2”, “r”);
outp = fopen(“merged-file”, “w”);
stat1 = fgets(buffer1, MAXLINE, in1p);
stat2 = fgets(buffer2, MAXLINE, in2p);
while (stat1 != NULL && stat2 != NULL) {
if (strcmp(buffer1, buffer2) < 0) {
fprintf(outp, “%s”, buffer1);
stat1 = fgets(buffer1, MAXLINE, in1p);
} else {
fprintf(outp, “%s”, buffer2);
stat2 = fgets(buffer2, MAXLINE, in2p);
}
}
while (stat1 != NULL) {
fprintf(outp, “%s”, buffer1);
stat1 = fgets(buffer1, MAXLINE, in1p);
}
while (stat2 != NULL) {
fprintf(outp, “%s”, buffer2);
stat2 = fgets(buffer2, MAXLINE, in2p);
}
fclose(in1p); fclose(in2p); fclose(outp);
return 0;
} Trang 16

8
12/2/2021

7-6 Software Engineering

In this section, we discuss some software engineering


issues related to files.

Topics discussed in this section:


Testing Files
Data Terminology

17

PROGRAM 7-11 Handling Errors—the Right Way

18

9
12/2/2021

PROGRAM 7-12 Handling Errors with Explanations

19

PROGRAM 7-12 Handling Errors with Explanations

20

10

You might also like