Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将值从ostringstream复制到字符串?_C++ - Fatal编程技术网

C++ 如何将值从ostringstream复制到字符串?

C++ 如何将值从ostringstream复制到字符串?,c++,C++,我试过: ostringstream oss; read a string from file and put to oss; string str; str << oss.str();// error here "error: no match for ‘operator>>’ in 'oss >> str' " ostringstream操作系统; 从文件中读取字符串并放入oss; 字符串str; str这没有任何意义oss.str()返回一个std::

我试过:

ostringstream oss;
read a string from file and put to oss;
string str;
str << oss.str();// error here "error: no match for ‘operator>>’ in 'oss >> str' "
ostringstream操作系统;
从文件中读取字符串并放入oss;
字符串str;

str
这没有任何意义
oss.str()
返回一个
std::string
。不能将
字符串
流式传输到
字符串
。您要么需要
str=oss.str()
,要么使用标准的
stringstream
,如果您试图将整个文件复制到stringstream,则执行
ss>
,然后执行以下操作:

oss << ifs;
如果只想获得一行,则跳过stringstream,只需使用getline:

std::getline(ifs,str);

@用户:也许你把垃圾放进去了?垃圾放进去了?你是什么意思?@user:你正在从你的字符串流中获取垃圾。可能是因为您将垃圾放入其中,代码表示为“从文件中读取字符串并放入oss”…ifstream ifs(“文件”);oss@user552279:这就是你的问题。这不是读取文件的正确方法。另外,为什么要尝试将文件读入流?很可能你可以直接从文件中读取(它也是一个流!)。将从文件中读取的代码添加到oss中。
oss << ifs.rdbuf();
str = oss.str();
std::getline(ifs,str);