Skip to content

5.Vlákna a synchronizace: Changed input code for MacOS

Suggested part of code for "read input" in MacOS wasn't fully correct.

Program reads valid input, but in case of existence of new-line character before end-of-transmission thinks, that input is invalid.



This behaviour can be explained by the fact, that feof function doesn't work properly by some reason and let the next iteration begin, where scanf tries to read number, which doesn't exist.

Let's check it by printing of next character in the beginning of each iteration:



Assumption was correct. I can't really explain what happens with feof and why it doesn't see end of file.

So the solution, which helped me to tackle this problem was exactly this one I've used to check taken chararacter: get it with getc and if it's valid one, then put it back by ungetc to work with it later. In this case, if we've taken an end-of-transmission, we simply can quit from cycle. Moreover we don't need now feof at all.



I truly hope, that can be helpful for next students to not to waste hours on trying to find problem in their program why it finishes with wrong return value :)))

My Mac configuration is:

MacBook Pro 15" 2017 -

macOS Ventura 13.1 (22C65)

Merge request reports