บทที่ 9 -9.5

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

9.

5 putc( ) getc( )
9.5.1 putc( )


putc(single_char,fp);

single_char single_character
fp file pointer
putc( )
9.1 putc( )
/* file1.c */
#include<stdio.h>
/* 1 */
#include<conio.h>
/* 2 */
#include<stdlib.h>
/* 3 */
void main(void)
/* 4 */
{
/* 5 */
FILE *fp;
/* 6 */
char ch;
/* 7 */
clrscr();
/* 8 */
if(( fp=fopen("a:testdata.dat","w"))==NULL)
/* 9 */
{
/* 10 */
printf("Error in open file");
/* 11 */
printf("\007");
/* 12 */
exit(1);
/* 13 */
}
/* 14 */
printf("Please press <Enter> to quit program.\n"); /* 15 */
printf("\nEnter your sentence : ");
/* 16 */
do
/* 17 */
{
/* 18 */

ch=getche( );
putc(ch, fp);
} while(ch !='\r'); /* \r = Enter */
fclose(fp);
}

/* 19 */
/* 20 */
/* 21 */
/* 22 */
/* 23 */

run a: testdata.dat



9.2

9.1
1 #include <stdio.h>
stdio.h fopen( ) 9
3 #include <stdlib.h> exit( ) 13
6 FILE *fp; fp
(file pointer) FILE *fp; file pointer
( a: testdata.dat fp

9 6
a: testdata.dat (write)
9 fp (NULL) fp

10 14
Error in open file bell
a fp
15
15 16 Enter

17 21 do ch
enter

22 fclose (fp);
9.5.2 getc( )


getc(fp);

single_char = getc(fp);

single_char single character


fp file pointer
getc( )
9.2 getc( )
/* file2.c */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(void)
{
FILE *fp;
char ch; clrscr();
clrscr();

/* 1 */
/* 2 */
/* 3 */
/* 4 */
/* 5 */
/* 6 */
/* 7 */
/* 8 */

if(( fp=fopen("a:testdata.dat","r"))==NULL)
{
printf("Error in open file");
printf("\007");
exit(1);
}
do
{
ch=getc(fp);
putchar(ch);
} while(ch !=EOF);
fclose(fp);
getch();
}

/* 9 */
/* 10 */
/* 11 */
/* 12 */
/* 13 */
/* 14 */
/* 15 */
/* 16 */
/* 17 */
/* 18 */
/* 19 */
/* 20 */
/* 21 */
/* 22 */

run a: testdata.dat

9.2
9.1

9.2
9.2 9.1
testdata.dat a:
bell
19 (ch !=
EOF); (EOF End Of File)

You might also like