Wednesday 27 June 2018

c - What can I use for input conversion instead of scanf?

I have very frequently seen people discouraging others from using scanf and saying that there are better alternatives. However, all I end up seeing is either "don't use scanf" or "here's a correct format string", and never any examples of the "better alternatives" mentioned.




For example, let's take this snippet of code:



scanf("%c", &c);


This reads the whitespace that was left in the input stream after the last conversion. The usual suggested solution to this is to use:



scanf(" %c", &c);



or to not use scanf.



Since scanf is bad, what are some ANSI C options for converting input formats that scanf can usually handle (such as integers, floating-point numbers, and strings) without using scanf?

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...