...
In this non-compliant code example, more than one character is pushed back on the stream referenced by fptr
fp
.
Code Block | ||
---|---|---|
| ||
FILE * fptrfp; char *file_name; /* initialize file_name */ fp = fopen(file_name, "rb"); if (fptrfp == NULL) { /* Handlehandle Error */ } /* Readread data */ ungetc('\n', fptrfp); ungetc('\r', fptrfp); /* Continuecontinue on */ |
Compliant Solution
If more than one character needs to be pushed by ungetc()
, then fgetpos()
and fsetpos()
should be used before and after reading the data instead of pushing it back with ungetc()
. Note that this solution can only be used if the input is seekable.
Code Block | ||
---|---|---|
| ||
FILE * fptrfp; fpos_t pos; char *file_name; /* initialize file_name */ fp = fopen(file_name, "rb"); fpos_t pos; if (fptrfp == NULL) { /* Handlehandle Error */ } /* Readread data */ if (fgetpos(fptrfp, &pos)) { /* Handlehandle Error */ } /* Readread the data that will be "pushed back" */ if (fsetpos(fptrfp, &pos)) { /* Handlehandle Error */ } /* Continue on */ |
...