|
|
1.1 root 1:
2:
3: fgetc() STDIO Function fgetc()
4:
5:
6:
7:
8: Read character from stream
9:
10: #include <stdio.h>
11: iinntt ffggeettcc(_f_p) FFIILLEE *_f_p;
12:
13: fgetc reads characters from the input stream fp. In general, it
14: behaves the same as the macro getc: it runs more slowly than
15: getc, but yields a smaller object module when compiled.
16:
17: ***** Example *****
18:
19: This example counts the number of lines and ``sentences'' in a
20: file.
21:
22:
23: #include <stdio.h>
24:
25:
26:
27: main()
28: {
29: FILE *fp;
30: int filename[20];
31: int ch;
32: int nlines = 0;
33: int nsents = 0;
34:
35:
36:
37: printf("Enter file to test: ");
38: gets(filename);
39:
40:
41:
42: if ((fp = fopen(filename,"r")) == NULL) {
43: printf("Cannot open file %s.\n", filename);
44: exit(1);
45: }
46:
47:
48:
49: while ((ch = fgetc(fp)) != EOF) {
50: if (ch == '\n')
51: ++nlines;
52:
53:
54:
55: else if (ch == '.' || ch == '!' || ch == '?') {
56: if ((ch = fgetc(fp)) != '.')
57: ++nsents;
58:
59:
60:
61:
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
67:
68:
69: fgetc() STDIO Function fgetc()
70:
71:
72:
73: else
74: while((ch=fgetc(fp)) == '.')
75: ;
76: ungetc(ch, fp);
77: }
78: }
79:
80:
81:
82: printf("%d line(s), %d sentence(s).\n",
83: nlines, nsents);
84: }
85:
86:
87: ***** See Also *****
88:
89: getc(), STDIO
90:
91: ***** Diagnostics *****
92:
93: fgetc returns EOF at end of file or on error.
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130: COHERENT Lexicon Page 2
131:
132:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.