c++ - 用 ifstream 读取 "*"

标签 c++ ifstream

我正在尝试从看起来像这样的文本文件中读取列

名称 number1*number2

为什么是这样

Float_t value=0;
ifstream ifs("values.dat");
string line;
while(std::getline(ifs, line)) // read one line from ifs
{
    istringstream iss(line); // access line as a stream
    string dataname;
    ifs >> dataname >> value; // no need to read further

但它无法读取“*number2”部分,即它仅将 number1 值传递给 xs。我该如何解决这个问题?

最佳答案

如果运算符周围没有空格,您可以将星号读作单个 char:

int number1, number2;
char op;
iss >> number1 >> op >> number2;

Demo.

关于c++ - 用 ifstream 读取 "*",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30920150/

相关文章:

c++ - 带或不带 istreambuf_iterator 的 ifstream 有什么区别?

c++ - 使用 ifstream 和 get() 从文件中获取一个字符串,用引号表示

c++ - ifstream 在 Xcode 中不起作用?

C++ ofstream 仅从 ifstream 输出最后一行数据

c++ - 避免切片异常类型 (C++)

c# - 在 C# 和 C++ 之间共享源文件

c++ - 使用 const 限定符将参数传递给模板函数时出错

c++ - 如何使用 ifstream 打开一个文件并一直读到最后

c++ - 为什么RGB888转RGB565多次会出现颜色丢失?

c++ - 为什么我在 clrscr(); 中遇到错误它说未定义?