File IO2
File IO2
Character Input/Output
Functions
1
12/2/2021
2
12/2/2021
3
12/2/2021
4
12/2/2021
10
5
12/2/2021
11
6
12/2/2021
printf(“File copied.\n”);
fclose(infilep);
fclose(outfilep);
Trang 13
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
8
12/2/2021
17
18
9
12/2/2021
19
20
10