使用istringstream提取
Extraction using istringstream?
我有以下内容:
char op; double x, y, z;
istringstream iss("v 1.0 2.0 3.0", istringstream::in);
iss>>op>>x>>y>>z;
但是在输出x,y和z的值时,它们都返回0?
更新:我猜它正在工作,但我输出它为:
int length=wsprintf(result," V is %d, %d, %d ", x, y, z);
TextOut(hdc,0,0,result,length);
没有显示正确的值
但是,如果值是int类型,例如:
char op; int x, y, z;
istringstream iss("v 1 2 3", istringstream::in);
iss>>op>>x>>y>>z;
%d
格式说明符期望是int
,但x
, y
和z
类型是double
。如果类型和格式说明符不匹配,则行为是未定义的。注意,wsprintf
的参考页面似乎没有任何double
的格式说明符。
建议使用std::wostringstream
和std::wstring
代替:
std::wostringstream ws;
ws << L" V is " << x << L"," << y << L"," << z;
const std::wstring result(ws.str());
TextOut(hdc,0,0,result.c_str(), result.length());
谢谢。
对于其他人,这是我为正确输出值所做的操作。
int length=sprintf_s(result," V is %0.1f, %0.1f, %0.1f ", x, y, z);
TextOut(hdc,0,0,result,length);
通用语法"%A"。"B"指小数点&前的A位;小数点后B位。
谢谢。
相关文章:
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么istream不支持右值提取
- 如何设置一个范围来提取我想要获得的信息
- 视觉工作室项目.提取源文件夹名称
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 从字符串中提取整数并形成一个数组
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- 从 std::vector<无符号字符>切片中提取 int?
- 在C++中使用重载提取运算符时出现问题
- 在 Qt(C++) 中使用 QProcess 解压缩 - 提取目录问题
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 无法从 std::string 中提取C++ Unicode 符号
- 如何从也包含C++字母的文本文件中提取某些数字?
- 如何从适合一定范围的数字中提取数字?
- C++从通用引用中提取实际类型
- 从运行服务的应用程序代码中提取窗口句柄
- 如何使用istringstream提取混合格式
- 使用istringstream提取