As an aside, in C programming with
scanf
, one should check the value returned, to see if it worked or not. This is mainly due to not knowing whether the input is in the correct format or not. The other thing is assumptions about consumption of white space can cause errors.
https://en.cppreference.com/w/c/io/fscanf
Note that all the versions return an
int
One should use the secure versions (with the _s suffix) to avoid problems like buffer overflow attacks.
If one wanted to be thoroughly pedantic, one could check the return value from
printf
as well.
In C++ std::printf is different from C's printf in that it is statically type safe. That is in the standard somewhere.
There are third party C++ formatted input/output library functions that are dynamically type and thread safe, with similar formatting options to printf/scanf. They are useful when there is a lot of format changes, one can avoid pages of normal C++ code, in favour of more dense printf like code. For example, try coding this with std::cout, all on 1 line of output, compare with printf style code:
All fields right justified
table boundary | ;
line number, 2 digits padded with 0;
field separator | ;
degrees, 3 digits, 0 padded;
degree symbol;
minutes, 2 digits, 0 padded;
minutes symbol ' ;
seconds, 2 digits, 0 padded;
seconds symbol " ;
field separator | ;
Distance width 9, 3 dp;
field separator | ;
Easting width 9, 3 dp;
field separator | ;
Easting adjustment, 2 digits with - sign if negative;
field separator | ;
Northing width 9, 3 dp;
field separator | ;
Northing adjustment, 2 digits with - sign if negative;
field separator | ;
EAST width 11, 3 dp;
field separator | ;
NORTH width 11, 3 dp;
Comment/ Description - as many chars available (left over) to fit when printed A4 landscape
table boundary | ;