标签: istringstream

如何检查流提取是否消耗了所有输入?

在下面的函数中,我尝试通过查看是否可以读取类型以及之后是否完全消耗输入来查看字符串s是否可转换为类型.我想要TT

template <class T>
bool can_be_converted_to(const std::string& s, T& t) 
{ 
  std::istringstream i(s);
  i>>std::boolalpha;
  i>>t;
  if (i and i.eof())
    return true;
  else
    return false;
}
Run Code Online (Sandbox Code Playgroud)

但是,can_be_converted_to<bool>("true")计算结果为false,因为i.eof()在函数结束时为false.

这是正确的,即使功能已经阅读整个字符串,因为它并没有试图看过去的字符串的结尾.(所以,显然这个函数适用于int和double,因为istringstream在阅读这些函数时会读到结尾.)

所以,假设我确实应该检查(i and <input completely consumed>):

问:如何使用eof()检查输入是否完全消耗?

c++ boolean eof istringstream

5
推荐指数
1
解决办法
390
查看次数

如何使用istringstream提取混合格式

为什么我的程序没有输出:

10
1.546
,Apple 1
Run Code Online (Sandbox Code Playgroud)

代替

10
1
<empty space>
Run Code Online (Sandbox Code Playgroud)

这是我的计划:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main () {
    string str = "10,1.546,Apple 1";
    istringstream stream (str);
    int a;
    double b;
    string c, dummy;
    stream >> a >> dummy >> b >> dummy >> c;
    cout << a << endl;
    cout << b << endl;
    cout << c << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

基本上我试图解析逗号分隔的字符串,任何更平滑的方式来做这将帮助我巨大.

c++ formatted-input istringstream

5
推荐指数
2
解决办法
3万
查看次数

std::ws(空格)在读取数据时的作用

保存在我的文件中的数据是(在此测试的开头和结尾都添加了空格):

1 2 3

使用以下带有或不带有“std::ws”的代码加载数据不会造成任何差异。所以我对“std::ws”的作用感到困惑,因为我看到了使用它的代码。有人可以解释一下吗?谢谢!

void main ()
{
ifstream inf; 
inf.open ("test.txt"); 

double x=0, y=0, z=0;
string line;

getline(inf, line);
istringstream iss(line);
//Using "std::ws" here does NOT cause any difference
if (!(iss >> std::ws >> x >> y >> z >> std::ws))
{
    cout << "Format error in the line" << endl;
}
else
{
    cout << x << y << z << endl;
}
iss.str(std::string ());
iss.clear();

cin.get();

}
Run Code Online (Sandbox Code Playgroud)

c++ whitespace stringstream getline istringstream

5
推荐指数
2
解决办法
9264
查看次数

do ... while()重复最后一个字符串两次

以下代码将提供的字符串/行拆分为字符.为什么循环重复最后一次字符串两次?怎么解决?

#include <iostream>
#include <vector>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    string main, sub;
    cout << "Enter string: ";
    getline(cin, main);
    istringstream iss(main);
    do
    {
        iss >> sub;
        cout << sub << endl;
        vector<char> v(sub.begin(), sub.end());
        for(int i = 0; i < v.size(); i++)
         {
             cout << v[i] << endl;
         }
    } while (iss);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输入:

你好,世界

期望的输出

你好
^ h
è


Ø
世界
w ^
ō
[R

d

实际产量:

你好 …

c++ do-while istringstream

5
推荐指数
1
解决办法
298
查看次数

如何将 ifstream 转换为 istringstream

我有一个函数可以读取 anistringstream并对其执行一些操作,所以...我尝试使用读取文件fstream并将fstream对象转换为 anistringstream以便将其传递给我的函数。我不知道该怎么做。任何帮助将非常感激。我仍然是一个初学者,所以如果可以的话请保持简单。

string inputFile(argv[1]);    
ifstream inFile(inputFile.c_str());    
istringstream is;

is >> inFile.rdbuf(); //*******This is wrong and is what I'm having trouble with

function(is);
Run Code Online (Sandbox Code Playgroud)

c++ fstream istringstream

5
推荐指数
1
解决办法
5777
查看次数

从 stringstream 读取具有特定字段宽度的 int

我正在尝试从字符串中以十六进制表示法读取字节。字节可能会或可能不会被空格分隔,例如,这" 00 ffab c "是一个有效的例子,应该导致读取 4 个字节,0x00、0xff、0xab 和 0x0c。问题是跳过空格但只读取两个相邻的数字(如果存在)。

如果输入是来自一个文件,任务将是一样容易while(fscanf(f, "%2d", &i) == 1) ...,因为sscanf空白,读出位置由基础跟踪跳跃FILE,和宽度仅应用到物品的最大场读,不包含空白原始输入字符。但是从字符串中读取时无法进行位置跟踪;我需要使用%n格式转换说明符,它将到目前为止通过此调用读取的字符数存储到关联变量中,例如scanf(f, "%2d%n", &i, &readIncr),并通过添加相应的增量手动维护读取位置。

这有点麻烦,因此我想使用std::istringstreamwhich跟踪底层字符串中的位置。

但是在输入流上设置宽度并没有预期的(和预期的)效果;下面是一个最小的演示;为简单起见,我使用十进制整数。输入字段宽度的文档和示例很少。

难道我做错了什么?我这个用例根本不是故意的?

#include <iostream>
#include <sstream>
#include <iomanip>
#include <cstdio>

using namespace std;

int main()
{
  const char *s = "   1234";
  int i;
  istringstream is(s);

  if (is >> setw(2) >> i)
  {
    cout << "stringstream i: " << i << '\n';
  } …
Run Code Online (Sandbox Code Playgroud)

c++ field width setw istringstream

5
推荐指数
1
解决办法
112
查看次数

Boost property_tree 错误:在 .ini 文件中获取元素时,将数据转换为类型“j”失败

我有一个 .ini 文件包含以下数据

[SYSTEM]
num_of_vps = 1
Run Code Online (Sandbox Code Playgroud)

我有这段代码来读取 .ini 文件中的元素。(uint定义为typedef unsigned int uint

boost::property_tree::ptree pt; 
boost::property_tree::ini_parser::read_ini(iniFilePath, pt);
hwCount = pt.get<uint>("SYSTEM.num_of_vps"); 
Run Code Online (Sandbox Code Playgroud)

我从包含上述代码的文件创建了一个文件,并在文件中的包装函数中调用了它main.cc。然后我收到以下错误

抛出 'boost_1_68_0::exception_detail::clone_impl<boost_1_68_0::exception_detail::error_info_injector<boost_1_68_0::property_tree::ptree_bad_data> >' 实例后调用的终止what():将数据转换为类型“j”失败的堆栈跟踪

#12 0x00002aaab613fcd5 in abort () from /lib64/libc.so.6
#13 0x00002aaab9b29315 in __gnu_cxx::__verbose_terminate_handler () at ../../../../src/gcc-7.3.0/libstdc++-v3/libsupc++/vterminate.cc:95
#14 0x00002aaab9a9e8f6 in __cxxabiv1::__terminate (handler=<optimized out>) at ../../../../src/gcc-7.3.0/libstdc++-v3/libsupc++/eh_terminate.cc:47
#15 0x00002aaab9a9e941 in std::terminate () at ../../../../src/gcc-7.3.0/libstdc++-v3/libsupc++/eh_terminate.cc:57
#16 0x00002aaab9a9ea74 in __cxxabiv1::__cxa_throw (obj=<optimized out>, tinfo=0x2aaab9e1ff60 <typeinfo for boost_1_68_0::exception_detail::clone_impl<boost_1_68_0::exception_detail::error_info_injector<boost_1_68_0::property_tree::ptree_bad_data> >>, dest=0x2aaab99bef18 <boost_1_68_0::exception_detail::clone_impl<boost_1_68_0::exception_detail::error_info_injector<boost_1_68_0::property_tree::ptree_bad_data> >::~clone_impl()>) at ../../../../src/gcc-7.3.0/libstdc++-v3/libsupc++/eh_throw.cc:93
#17 0x00002aaab99bec82 in boost_1_68_0::throw_exception<boost_1_68_0::exception_detail::error_info_injector<boost_1_68_0::property_tree::ptree_bad_data> …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-propertytree istringstream c++14

5
推荐指数
1
解决办法
2596
查看次数

为什么 std::stringstream 不能与 std::string_view 一起使用?

初始化std::stringstream 构造函数接受const string&以下参数:

explicit stringstream (const string& str,
                       ios_base::openmode which = ios_base::in | ios_base::out);
Run Code Online (Sandbox Code Playgroud)

这个接口在 C++98 中是合理的,但从 C++17 开始,我们有了std::string_view表示字符串的类的更便宜的替代方案。该类std::stringstream不会修改它接受的字符串,不拥有它,也不要求它以空终止。那么为什么不添加另一个接受 的构造函数重载呢std::string_view?是否存在任何障碍使该解决方案不可能(或不合理)屈服于Boost::Iostreams等替代方案?

c++ istringstream string-view c++17

5
推荐指数
1
解决办法
1250
查看次数

如何从矢量char初始化字符串流

目前我正在使用boost char数组

boost::array<char, 512> received_data;
std::istringstream ss_(received_data.data()); 
Run Code Online (Sandbox Code Playgroud)

但是如果我的received_data是一个 std::vector<char> received_data(512);

我怎么才能把这些数据发给我std::istringstream ss_

c++ string vector istringstream

4
推荐指数
1
解决办法
2637
查看次数

如何将istringstream和ifstream分配给istream变量?

我想要一个类型istream可以容纳文件或字符串内容的变量。想法是,如果未指定文件,则将为类型的变量istream分配一个字符串。

std::ifstream file(this->_path)
Run Code Online (Sandbox Code Playgroud)

std::istringstream iss(stringSomething);
Run Code Online (Sandbox Code Playgroud)

std::istream is
Run Code Online (Sandbox Code Playgroud)

我尝试过istream像将它们分配给变量一样,将它们分配给从同一基类继承的其他对象,但这没有用。

如何分配istringstreamifstream一个istream变量?

c++ istream ifstream istringstream

4
推荐指数
2
解决办法
6056
查看次数