如何在c++中使用scanf读取线条
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("%dn%lfn%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
函数,但我不建议这样做,因为缓冲区可能会溢出,尤其是对于像get line这样的函数。
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 如何通过 scanf() 读取没有标点符号的字符串?
- SCANF 读取错误,因为使用 %d 读取布尔值
- 使用 scanf 读取文件中的图形时出现问题
- 如何从文件中一次读取一个输入,类似于在c++中使用cin/scanf从控制台读取输入
- 程序使用SCANF读取字符串时会崩溃
- C 在scanf()读取后删除指针导致过程终止
- 从scanf读取C样式字符串时出现分段错误
- 使用 scanf 读取C++字符串
- 从管道读取输入时出现scanf问题
- SCANF 格式说明符仅读取数字
- scanf 只能将 3 个字符读取到 5 位字符数组中
- 为什么我不能使用此 scanf 函数读取输入字符串?
- c/c++scanf/cin从多行读取int
- 从scanf读取换行符
- c++的新功能,scanf不能正确读取/打印输入
- 使用 scanf("%c, &ch) 读取字符;
- 读取文件:如何使用fscanf移动文件指针,以及scanf/fscanf如何处理丢失的%类型