使用2D数组读取CSV文件 [英] Read csv file using 2d array

查看:84
本文介绍了使用2D数组读取CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用2d数组读取CSV文件,但是读取存在问题。文件的第一个单元格将被跳过,然后继续读取所有内容。我不明白为什么它不读取第一个单元格。

I'm trying to read a CSV file using 2d array but there's a problem with the reading. The first cell of the file is skipped and then continues read all. I don't understand why it doesn't read the first cell.

#include<iostream>
#include<fstream>
#include<cstring>
#include<string>
#include<sstream>
using namespace std;

int main()
{
  string arrival,job[3][4];
  ifstream jobfile("myfile.csv");
  std::string fileCommand;

  if(jobfile.is_open())
  {
      cout << "Successfully open file"<<endl;

      while(getline(jobfile,arrival,','))
      {
        for(int i=1;i < 4;i++) //i = no. of job
        {
            for(int j=0; j<4; j++) // j = no. of processes
            {
                getline(jobfile,job[i][j],',');
                cout << "Job[" << i << "]P[" << j << "]: "<< job[i][j]<< endl;
            }

        }//end for
      }//end while
  }//end if for jobfile open
  jobfile.close();    
}


推荐答案

更改此内容:

for(int i=1;i < 3;i++)

为此:

for(int i=0;i < 3;i++)

此外,删除此 getline(jobfile,job [ i] [j],','); ,因为您以这种方式跳过了一行。在while循环的条件下调用getline时,它已经读取了一行(因此,现在必须存储该行。然后,当while循环的条件再次被求值时,将读取下一行)。

Also, remove this getline(jobfile,job[i][j],',');, since you skip a line that way. When you called getline in the condition of the while loop, it already read a line (as a result, now, you have to store that line. Then, when the condition of the while loop is evaluated again, the next line will be read).

但是,由于您到达一次将保留一个令牌,直到遇到当前行的最后一个令牌。在这种情况下,到达将是这样: currentLineLastToken\nnextLineFirstToken

However, it gets much more complicated than this, since you arrival will hold one token at a time, until it meets the last token of the current line. In that case, arrival will be this: "currentLineLastToken\nnextLineFirstToken".

因此,您需要专门处理到达包含换行符的情况,请使用为此, string :: find

For that reason, you need to specially handle the case that arrival contains a newline, use string::find for this.

找到换行符,您应该将该字符串拆分为该换行符,以便提取所涉及的两个标记。使用 string :: substr

When a newline is found, you should split that string to that newline, in order to extract the two tokens involved. Use string::substr for this.

此外,您不应该在while循环内循环使用双精度来存储令牌,您只需阅读。仅在退出读取文件的while循环之后,才需要打印 job 时,使用double for循环。

Moreover, you shouldn't loop inside the while loop with a double for to store the token, you just read. Use a double for loop, when it is time to print job, only after exiting the while loop that read the file.

将所有内容放在一起,我们得到:

Putting everything together, we get this:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
  string arrival,job[3][4];
  ifstream jobfile("myfile.csv");
  std::string fileCommand;

  if(jobfile.is_open())
  {
      cout << "Successfully open file"<<endl;

      int i = 0, j = 0;
      while(getline(jobfile,arrival,','))
      {
        //cout << "|" << arrival << "|" << endl;
        size_t found = arrival.find("\n");
        if (found != std::string::npos) // if newline was found
        {
                string lastToken = arrival.substr(0, found);
                string nextLineFirstTOken = arrival.substr(found + 1);
                job[i++][j] = lastToken;
                j = 0;
                if(nextLineFirstTOken != "\n") // when you read the last token of the last line
                        job[i][j++] = nextLineFirstTOken;
        }
        else
        {
                job[i][j++] = arrival;
        }

      }//end while

      for(int i = 0; i < 3; ++i)
      {
        for(int j = 0; j < 4; ++j)
        {
                cout << job[i][j] << " ";
        }
        cout << endl;
      }

  }//end if for jobfile open
  jobfile.close();
}

输出(用于我的自定义输入):

Output (for my custom input):

Successfully open file
aa bb cc dd 
bla blu blo ble 
qq ww ee rr

这篇关于使用2D数组读取CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆