使用stringstream库解析行时出现std :: getline问题

时间:2016-11-26 23:32:35

标签: c++

我在尝试从文件中读取字符串流时遇到错误,我似乎无法弄清问题是什么。

我收到的错误是:

  no matching function for call to 'getline'
  std::getline(parse_input, intensity, ',');

对于我要解析的每个变量都会重复。

以下是我的代码:

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

int main(int argc, char const *argv[])
{
  std::fstream fs;
  fs.open("test.dat");

  std::string* configuration_record;
  std::string temp_count;
  size_t no_of_records;

  if(!fs.is_open())
  {
    std::cerr << "File failed to open" << std::endl;
  }
  else
  {
    while(!fs.eof())
    {
      std::getline(fs, temp_count);
      no_of_records++;
    }
    fs.clear();
    fs.seekp(0);
  }

  std::string stimulation_type;
  std::string stimulation_name;
  double intensity;
  double frequency;
  double duration;
  std::string location;

  configuration_record = new std::string[no_of_records];

  for(size_t i=0; i<no_of_records; i++)
  {
    std::getline(fs, configuration_record[i]);
  }

  for(size_t i=0; i<no_of_records; i++)
  {
    size_t found;
    found = configuration_record[i].find("stim");
    if(found != std::string::npos)
    {

      std::stringstream parse_input(configuration_record[i]);
      std::getline(parse_input, stimulation_type, ',');
      std::getline(parse_input, stimulation_name, ',');
      std::getline(parse_input, location, ',');
      std::getline(parse_input, intensity, ',');
      std::getline(parse_input, frequency, ',');
      std::getline(parse_input, duration, ',');

      std::cout << stimulation_name << "," <<location << intensity << "," << frequency << "," << duration
      << std::endl;
    }
    else
    {

      std::stringstream parse_input(configuration_record[i]);
      std::getline(parse_input, stimulation_type, ',');
      std::getline(parse_input, stimulation_name, ',');
      std::getline(parse_input, intensity, ',');
      std::getline(parse_input, duration, ',');

      std::cout << stimulation_name << "," << intensity << "," << duration
      << std::endl;
    }
  }

  return 0;
}

1 个答案:

答案 0 :(得分:0)

来自manual的getline的声明是:

if userInput1 == "create"

如您所见,他们接受createstringstream有效),但第二个参数必须是istream& getline (istream& is, string& str, char delim); istream& getline (istream&& is, string& str, char delim); istream& getline (istream& is, string& str); istream& getline (istream&& is, string& str); ,因此当您通过istream时({ {1}},stringdouble),您正在调用一个不存在的函数。 您可以使用临时字符串,然后使用stod

将其保存为double
intensity