I train myself in building a compiler. When I read the file I sometimes need to look a few characters ahead of my current position to know which token I have to generate.
There are two options that come to my mind in that case:
- I read the entire file first and access the characters with an index variable
- I read one char at the time with
getc(file);and in case I have to go back to some previous character I usefseek(file, -1, SEEK_CUR);
Which one of these options is more efficient? Which would you prefer?
Thanks for the comments. My decision is to just read the file entirely first and then later check if I run into any performance issues.