C ++读取两个带有不同定界符的文件

问题描述 投票:0回答:1

我是编程新手,不胜感激我的当前问题:我想读取两个不同的文件:inputA和inputB,其中一个具有此定界符“:”,另一个具有定界符“,”。目的是获取inputA和inputB的值,并将它们组合到outputC。我已经能够单独读取文件(并将它们添加到二维数组arrayA或dataB中)。但是我想将两个脚本合并为一个。因此,想法是在inputA使用':'打开getline的同时将值存储到dataA中,而inputB在使用','打开getline的并将其存储到dataB。这就是我的脚本看起来可以读取inputA或inputB的样子:

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

using namespace std;

typedef vector <double> record_t;
typedef vector <record_t> data_t;

istream& operator >> ( istream& ins, record_t& record )
  {
  record.clear();
  string line;
    getline( ins, line );
  stringstream ss( line );
  string field;
  while (getline( ss, field, ':' )) // use ',' when inputB is supposed to be read
    {
    stringstream fs( field );
    double f = 0.0;
    fs >> f;
    record.push_back( f );
    }
  return ins;
  }

istream& operator >> ( istream& ins, data_t& data ) // here distinction between dataA or dataB?
  {
  data.clear();
  record_t record;
  while (ins >> record)
    {
    data.push_back( record );
    }
  return ins;
  }

int main( int argc, char* argv[] )
{
  data_t data;
  ifstream infile( "inputA.pgm" ); // and inputB.csv should be here
  infile >> data;
  if (!infile.eof())
    {
    cout << "file could not be opened\n";
    return 1;
    }
{
  infile.close();

//Output data
      ofstream myfile;
      myfile.open ("output.csv");
      myfile << data[1][1] << "," << data[2][1] << "," << data[3][1] << "\n"; //output dataA 
      myfile << data[1][1] << "," << data[2][1] << "," << data[3][1] << "\n"; //output dataB 
      myfile.close();

     return 0;
}
}

谢谢!

c++ operators delimiter ifstream getline
1个回答
0
投票

像这样搜索使用的分隔符:

istream& operator >> ( istream& ins, data_t& data ) 
// here distinction between dataA or dataB? Yes it should happen here :D
{
    char delimiter = find_used_delimiter(ins);
    data.clear();
    record_t record;
    while (foo(ins, record, delimiter)) // use here a function where you read the record and pass the char-delimiter
        data.push_back( record );

   return ins;
}

char find_used_delimiter(istream& ins) {
    string line;
    std::size_t found = line.find_first_not_of("0123456789."); // search for first char, which is not a double char

   if (found!=std::string::npos)
   {
       std::cout << "The first non-double character is " << line[found];
       std::cout << " at position " << found << '\n';
       return line[found];
   }

   // here you should do some error handling, if there is no delimiter
}

istream& foo(istream& ins, record_t& record, char delimiter) {
    record.clear();
    string line;
    getline( ins, line );
    stringstream ss( line );
    string field;
    while (getline( ss, field, delimiter )) // use ',' when inputB is supposed to be read
    {
        stringstream fs( field );
        double f = 0.0;
        fs >> f;
        record.push_back( f );
    }
    return ins;
 }

警告,该代码未经测试,应更多地作为指南。供参考:find_first_not_of

© www.soinside.com 2019 - 2024. All rights reserved.