|
|
1.1 root 1:
2:
3: fgets() STDIO Function fgets()
4:
5:
6:
7:
8: Read line from stream
9:
10: #include <stdio.h>
11: cchhaarr *ffggeettss(_s, _n, _f_p) cchhaarr *_s; iinntt _n; FFIILLEE *_f_p;
12:
13: fgets reads characters from the stream fp into string s until
14: either _n-1 characters have been read, or a newline or EOF is en-
15: countered. It retains the newline, if any, and appends a null
16: character at the end of of the string. fgets returns the argu-
17: ment _s if any characters were read, and NULL if none were read.
18:
19: ***** Example *****
20:
21: This example looks for the pattern given by argv[1] in standard
22: input or in file argv[2]. It demonstrates the functions pnmatch,
23: fgets, and freopen.
24:
25:
26: #include <stdio.h>
27: #define MAXLINE 128
28: char buf[MAXLINE];
29:
30:
31:
32: void fatal(s) char *s;
33: {
34: fprintf(stderr, "pnmatch: %s\n", s);
35: exit(1);
36: }
37:
38:
39:
40: main(argc, argv)
41: int argc; char *argv[];
42: {
43: if (argc != 2 && argc != 3)
44: fatal("Usage: pnmatch pattern [ file ]");
45:
46:
47:
48: if (argc==3 && freopen(argv[2], "r", stdin)==NULL)
49: fatal("cannot open input file");
50:
51:
52:
53: while (fgets(buf, MAXLINE, stdin) != NULL) {
54: if (pnmatch(buf, argv[1], 1))
55: printf("%s", buf);
56: }
57:
58:
59:
60:
61:
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
67:
68:
69: fgets() STDIO Function fgets()
70:
71:
72:
73: if (!feof(stdin))
74: fatal("read error");
75: exit(0);
76: }
77:
78:
79: ***** See Also *****
80:
81: fgetc(), gets(), STDIO
82:
83: ***** Diagnostics *****
84:
85: fgets returns NULL if an error occurs, or if EOF is seen before
86: any characters are read.
87:
88:
89:
90:
91:
92:
93:
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.