Efficiency reading chars of a file

71 Views Asked by At

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 use fseek(file, -1, SEEK_CUR);

Which one of these options is more efficient? Which would you prefer?

1

There are 1 best solutions below

0
MrKleeblatt On BEST ANSWER

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.