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.

1.png

2.png

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:

3.png

4.png

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.

5.png

6.png

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 - https://everymac.com/systems/apple/macbook_pro/specs/macbook-pro-core-i7-2.8-15-mid-2017-retina-display-touch-bar-specs.html

macOS Ventura 13.1 (22C65)

Merge request reports