Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
7184 | siemargl | 1 | /* ungetc example */ |
2 | #include |
||
3 | |||
4 | void trace_file(FILE* f, char* cmt); |
||
5 | |||
6 | int main () |
||
7 | { |
||
8 | FILE * pFile; |
||
9 | int c; |
||
10 | char buffer [256]; |
||
11 | |||
12 | pFile = fopen ("myfile.txt","rt"); |
||
13 | if (pFile==NULL) perror ("Error opening file"); |
||
14 | else while (!feof (pFile)) { |
||
15 | trace_file(pFile, "1"); |
||
16 | |||
17 | c=getc (pFile); |
||
18 | |||
19 | trace_file(pFile, "before ungetc"); |
||
20 | |||
21 | if (c == EOF) break; |
||
22 | if (c == '#') ungetc ('@',pFile); |
||
23 | else ungetc (c,pFile); |
||
24 | |||
25 | trace_file(pFile, "after"); |
||
26 | |||
27 | if (fgets (buffer,255,pFile) != NULL) |
||
28 | puts (buffer); |
||
29 | else break; |
||
30 | } |
||
31 | return 0; |
||
32 | } |
||
33 | |||
34 | void trace_file(FILE* f, char* cmt) |
||
35 | { |
||
36 | printf("%s[%s]\n", cmt, f->buffer); |
||
37 | printf("mode=%0X, filesize=%d, filepos=%d\n", f->mode, f->filesize, f->filepos); |
||
38 | printf("ungetc=%d, buffer_start=%d, buffer_end=%d\n", f->ungetc_buf, f->buffer_start, f->buffer_end); |
||
39 | } |