[英]How to read a line using scanf in c++?
我正在尝试使用“scanf”来读取字符串行:“它不起作用吗”,但我不知道在这个特定示例中是否有可能实现它。
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
using namespace std;
int main() {
int i = 4;
double d = 4.0;
string s = "Just an example of, why ";
int number;
double doub;
string longText;
scanf("%d %lf %s", &number, &doub, &longText); //Read a line ex ("is it not working")
printf("%d\n%lf\n%s", number+i, doub+d,s+longText); //Print all the values, but not printing s+longText
}
printf
/ scanf
系列中%s
代表 char 数组,而不是std::string
。 如果你想获得线路,请使用std::getline
。 如果你想为 stdio 函数使用std::string
缓冲区,请使用std::string::data
function,但我不建议这样做,因为缓冲区溢出是可能的,尤其是对于像 get-line 这样的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.