scnlib
is a modern C++ library to replace scanf
and std::istream
. This library tries to bring us even closer to the complete replacement of iostream
and stdio
in C.
It’s faster than iostream
and typed, unlike scanf
. Think of {fmt} or std::format
in C++20, but in another direction.
This library is the reference implementation of the ISO C++ standards proposal P1729 “Text Parsing”.
To compile, simply clone with git and use CMake and install:
git clone https://github.com/eliaskosunen/scnlib
cd scnlib
cmake -B build .
cmake --build build
sudo cmake --install build
After that, you can exit the repository and remove it:
cd .. && rm -rf scnlib
.
In this example, in addition to scnlib
, there is also the use of println
from C++23, we will read 2 numbers via prompt and their sum will automatically be displayed:
main.cpp
#include <scn/scan.h>
#include <print>
constexpr auto sum = [](int x, int y){
return x + y;
};
int main(){
if(auto result =
scn::prompt<int, int>("Enter 2 numbers to add? ", "{} {}")){
auto [a, b] = result->values(); std::println("The sum of {} plus {} is: {}", a, b, sum(a, b));
}else{
std::println(stderr, "Error: {}", result.error().msg());
}
}
To compile, use the flag: -lscn
:
g++ main.cpp -lscn
Then, run the binary and test, example:
./a.out
Enter 2 numbers to add? 3 6
The sum of 3 plus 6 is: 9
For more information, access the official repository and the address: https://scnlib.dev/.