Tuesday 15 September 2015

Looking one char ahead when reading from file in C -



Looking one char ahead when reading from file in C -

i aware in c there functions getc() , ungetc().

i need counter-function fgetc(), sadly unfgetc() doesn't exist. tried writting on own.

this how looks:

int getnextchar(file* fd) { // character int nextcharacter = fgetc(fd); // fseek back, don't move file descriptor fseek(fd, -1, seek_cur); // returning char (as int) homecoming nextcharacter; }

but well... doesn't seem work. phone call within while loop, this.

while ( (c = fgetc(fd)) != eof) { cx = getnextchar(fd); printf("%c", c); }

it gets stucked on lastly character of file (it prints every iteration infinity). little explanation why need in case i'm doing wrong , there suitable solution.

i need check next character on beingness eof. if eof, forcefulness send token, created in while loop (this part not of import issue, didnt include it).

i going through loop , whenever find character doesnt respond mask, assume should send token , start making new 1 character doesnt respond. naturally, when read lastly char in file, no next iteration done, hence won't send lastly token. need check next char eof. if eof, forcefulness send token.

thank advices!

you need check nextcharacter isn't eof, since if is, you'll still off, causing outer reading never see end of file. check homecoming values of more functions, fseek().

c file loops

No comments:

Post a Comment