PDA

View Full Version : کار تابع () ungetc چیست؟



mahdi-1
پنج شنبه 01 فروردین 1392, 18:27 عصر
سلام
میشه کار تابع ungetc رو به صورت مفهومی توضیح بدین؟

maktoom
پنج شنبه 01 فروردین 1392, 20:33 عصر
سلام
ازونجاییکه الان عیده و ممکنه متخصصین حضور نداشته باشن که بهتون پاسخی بدن،بنده جرات کردم و این رو براتون میذارم.
/* ungetc example */
#include <stdio.h>

int main ()
{
FILE * pFile;
int c;
char buffer [256];

pFile = fopen ("myfile.txt","rt");
if (pFile==NULL) perror ("Error opening file");
else while (!feof (pFile)) {
c=getc (pFile);
if (c == EOF) break;
if (c == '#') ungetc ('@',pFile);
else ungetc (c,pFile);
if (fgets (buffer,255,pFile) != NULL)
fputs (buffer,stdout);
else break;
}
return 0;
}
و در ادامه این توضیح هستش:
This example opens an existing file called myfile.txt for reading and prints its lines, but first gets the first character of every line and puts it back into the stream replacing any starting # by an @.
منبع (http://www.cplusplus.com/reference/cstdio/ungetc/)