getline() 跳过第一行和最后一行之间每一行的第一列

getline() skipping first column of every line between the first and last line

本文关键字:一行 一列 之间 最后 getline      更新时间:2023-10-16

我已经看了2天了这段代码,但仍然找不到我出错的地方。在线搜索并应用了所有方法,但它仍然跳过第一列。例如,如果我有 first line ->1,2,3,4 second line->5,6,7,8 and third line->9,10,11,12. The output it gives is first line ->1,2,3,4 second line->6,7,8 and third line->9,10,11,12. 并且不知道为什么它也将它们存储在一个向量而不是 2d 向量中,但这不是真正的问题,因为我可以解决它。非常感谢您的帮助。

#include <iostream>
#include <fstream>
#include <sstream>
#include<string>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<int> > array;
vector<vector<int> > readCSV(string filename)
   {
     vector<vector<int> > contents;
     vector<int> line_contents; 
     ifstream in(filename);
     if(in.is_open()){
      string line;
    while(getline(in, line))
{
  line_contents.clear();
  // contents.push_back(vector<int>());
  //vector<int> line_contents;// = contents[contents.size()-1];  
istringstream iss(line);
    do
  {
        int value;
    //line_contents.clear();
    //  iss.ignore();
    //string fieldend;
    // getline(iss, fieldend, ','); 
    if( iss >> value )
     {
            line_contents.push_back(value);
      }
    else
      {
            iss.clear();
      }
    // iss.clear();
        string fieldend;
        getline(iss, fieldend, ',');
       // read to separator
  } while(!iss.eof());
array.push_back(line_contents); 
//contents.push_back();
//contents.push_back( vector<int>());
 }
 in.close();
}
 else{
cout<<"Couldn't open input file... make sure the file is in the cwd"<<endl;
}
 return contents;
}
  int main()
 {
   //  int size;
     int max=0;
   ofstream F1 ("inputDBX.csv");
    ofstream F2 ("inputDBX2.csv");
  readCSV("inputDB.csv");
  if(F1.is_open() && F2.is_open()){
 // print the array
  for(int i=0; i<array.size();)
   {
    F1<<array[0][i]<<","<<array[0][i+1]<<","<<array[0][i+2]<<","<<array[0] [i+1]<<","<<array[0][i+2]<<endl;
   if (max< array[0][i+2]){
  max=  array[0][i+2];
  }
    else{
F2<<array[0][i]<<","<<"0.0"<<","<<array[0][i+2]<<","<<array[0][i+1]<<","<<max+0.5<<endl;
  }
  i=i+3;
}
 F1.close();
 F2.close();
}
 else{
cout<<"Unable to open/create one/both output files;"<<endl;
}
 //  cout<<array.size()<<"x"<<array[1].size()<<endl;     
  for(int i=0; i<array.size(); ++i)
{
for(int j=0; j<array[i].size(); ++j)
  {
cout << array[i][j] <<" ";
  }
cout << endl;
}
return 0;      
 }
最后,

我通过更改代码开始工作。通过替换

   while(getline(in, line))
 {
   line_contents.clear(); 
   istringstream iss(line);
  do
  {
    int value;
if( iss >> value )
 {
        line_contents.push_back(value);
  }
else
  {
        iss.clear();
  }
// iss.clear();
    string fieldend;
    getline(iss, fieldend, ',');
   // read to separator
  } while(!iss.eof());
array.push_back(line_contents); 
 }

有了这个:

while(getline(in, line))
{
  line_contents.clear();
istringstream iss(line);
    int value,value2,value3;
    char valuea,valueb;
    do{
    if( iss >> value >> valuea >>value2>> valueb>>value3)
    {
        line_contents.push_back(value);
        line_contents.push_back(value2);
        line_contents.push_back(value3);
    }}while(!iss.eof());
array.push_back(line_contents); 
 }

通过格式化提取手动读取所有值