For example...
char* foo;
scanf("%[^\n\r]", foo);
How can I do this in C++, without including C libraries?
For example...
char* foo;
scanf("%[^\n\r]", foo);
How can I do this in C++, without including C libraries?
The equivalent to what you have posted [aside from the fact that char *foo
without an allocation of memory, probably will lead to writing to either NULL or some random location in memory] would be
std::string foo;
std::getline(std::cin, foo);
But for more complex cases, where you read multiple items, either cin >> x >> y >> z;
or std::getline(std::cin, str); std::stringstream ss(str); ss >> x >> y >> z;
- or some combination thereof.
But C-code that is valid is valid in C++ too. It's not always the "right" solution, but certainly not completely wrong either.
The C++ version of
scanf
isstd::scanf
and can be found in the<cstdio>
header. Yes, it's the same function - because C functions can also be used in C++.