0

I'm trying to implement an evaluation of the arithmetic expression algorithm for my exam in C. I have a text file with the expression e.g "12 - 2 + cos(8)" in "r" mode. In "while" cycle I read 1 character and then apply switch on it. When I need to evaluate a function for example "cos" or "sin" it does the following:

case 'c':
         fgetpos(fp, &position);
         position -= 1;
         fsetpos(fp, &position);
         fgets(math_function, 4,fp);

Then I have math_function = "cos" and can work with it. But my teacher says that I can't use fsetpos because it's not secure and compares it with function fseek which can be used only on binary files. He refers to documentation, but I didn't find anything that can prove it or otherwise. How can I convince him or I am forbidden to use it in text files? Thanks in advance

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
kostuyk21
  • 5
  • 2
  • 2
    Rather than processing the expression while you're reading from the file, read the whole expression into a string and then process the string. That's much simpler. – Jabberwocky Mar 31 '22 at 13:36
  • Or consider using `ungetc()`. – Weather Vane Mar 31 '22 at 13:37
  • All that's guaranteed by the C specification is that [`fpos_t`](https://en.cppreference.com/w/c/io/fpos_t) isn't an array type. It might as well be a structure. Therefore you can't reliably do any modifications to an `fpos_t` variable as its actual type is unknown. It can really only be used to get the current position by [`fgetpos`](https://en.cppreference.com/w/c/io/fgetpos) and then reset the position with [`fsetpos`](https://en.cppreference.com/w/c/io/fsetpos). – Some programmer dude Mar 31 '22 at 13:38
  • 1
    How do you know `c` matches to `cos`? Can't it be `cis` or `csq` or `ctg` or something else? – pmg Mar 31 '22 at 13:41
  • @Jabberwocky I also have written a program with a text file and dictionary where if a word exists in the dictionary in the file it's replaced by "*****". So I assume I can't read the whole file to a sting so what can I do? – kostuyk21 Mar 31 '22 at 13:52
  • @kostuyk21 what?? I don't get it. There may be bugs in your code... who knows. Maybe you should ask a separate question dealing specifically with _that_ pproblem? – Jabberwocky Mar 31 '22 at 13:57

0 Answers0