如何使用C ++读取.csv文件并以另一种形式输出? [英] How to use C++ to read in a .csv file and output in another form?

查看:455
本文介绍了如何使用C ++读取.csv文件并以另一种形式输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.csv文件,有3行和5列,值为0,1,2,3,50或100.我将其从Excel表保存到.csv文件。我试图使用C ++读取.csv文件,并输出.csv文件中的前两个列值为基于最后三个列值的文本文件。我假设.csv文件看起来像



1,1,value,value,value



2,value,value,value



1,3,value,value,value





我查看了从.csv文件中的字段读取值?,并从中使用一些代码。



这是我的代码:

  #include< iostream> 

#include< fstream>

using namespace std;

char separator;
int test_var;

struct Spaxel {
int array1;
int array2;
int red;
int blue_o2;
int blue_o3;
};

Spaxel whole_list [3];

int main()
{
//读入文件
ifstream myfile(sample.csv);
Spaxel data;
int n = 0;
cout<< data.array1<< endl
myfile>> data.array1; // using as a test to see if it is working
cout<< data.array1<< endl
while(myfile>> data.array1)
{
//存储5个变量并除去逗号
cout<<here?< ; endl
//跳过分隔符,例如comma(',')
myfile>>分隔器;

//读入下一个值。
myfile>> data.array2;

//跳过分隔符
myfile>>分隔器;

//读入下一个值。
myfile>> data.red;

//跳过分隔符,例如comma(',')
myfile>>分隔器;

//读入下一个值。
我的文件>> data.blue_o2;

//跳过分隔符
myfile>>分隔器;

//读入下一个值。
myfile>> data.blue_o3;

//忽略换行符,因为它仍然在缓冲区中。
myfile.ignore(10000,'\\\
');

//将数组中的值存储到以后打印到另一个文件中
whole_list [n] = data;
cout<< whole_list [n] .red< endl
n ++;

}
myfile.close();

//将整个列表的内容放在输出文件中
//whole_list[0].red = whole_list [0] .array1 = whole_list [0] .array2 = 1;这是一个测试,它没有工作
的流输出(sample_out.txt);
for(int n = 0; n <3; n ++){
if(whole_list [n] .red == 1)
output< whole_list [n] .array1<<,< whole_list [n] .array2< endl
}
return 0;
}



当我在Xcode中运行它时打印三个0(从cout& < data.array1<< endl; cout<< data.array1<< endl;在main()的开头和返回0),但不输出任何文件。显然.csv文件没有被正确读取,并且输出文件没有被正确写入。任何建议?



感谢您的时间!

解决方案

您提供的代码中的几个问题区域:




  • 硬编码文件名。在没有sample.csv的目录中运行程序可能会导致 ifstream 失败。

  • 无法检查 myfile 是否成功打开。

  • Loop可以访问



下面的重构代码,而不是完全万无一失,纠正了许多提到的问题。

  #include< iostream> 
#include< vector>
#include< fstream>
#include< sstream>
using namespace std;


struct Spaxel
{
int array1;
int array2;
int red;
int blue_o2;
int blue_o3;
};

ostream&运算符<< (ostream& os,const Spaxel& rhs)
{
os< rhs.array1
<< ','
<< rhs.array2
<< ','
<< rhs.red
<< ','
<< rhs.blue_o2
<< ','
<< rhs.blue_o3;

return os;
}

istream& operator>> (istream& is,Spaxel& rhs)
{
char delim;
为>> rhs.array1
>> delim
>> rhs.array2
>> delim
>> rhs.red
>> delim
>> rhs.blue_o2
>> delim
>> rhs.blue_o3;

return is;
}


int main(int argc,const char * argv [])
{
if(argc <2)
{
cout<< 用法:< argv [0]<< filename\\\
;
return 1;
}

const char * infilename = argv [argc - 1];
//读入文件
ifstream myfile(infilename);
if(!myfile)
{
cerr<< 无法打开文件<<英文
return 1;
}

vector< Spaxel>整体
string line;
while(getline(myfile,line))
{
Spaxel data;
stringstream linestr(line);
linestr>>数据;
whole_list.push_back(data);

cout<<数据< '\\\
';
}
}

编辑



因为你知道 main 是你的程序的入口点,所以它是你自己的代码调用的东西。额外的可选参数 int argc,const char * argv [] 是在使用参数运行程序时传递的选项和参数。第一个参数 argc 表示有多少参数被传入。第二个 argv 是一个数组 char * ,每个元素都是传递的参数。第一个参数 argv [0] 是您的程序名称,因此 argc 始终为 = 1



假设您从shell中执行 p>


./ sample sample.csv


argc argv 将具有以下内容:

  argc = 2; 
argv [0] =sample
argv [1] =sample.csv

所以 const char * infilename = argv [argc - 1]; 获取传入的最后一个参数,应该是要读入的文件名。

I have a .csv file that has 3 rows and 5 columns with values of 0,1,2,3,50, or 100. I saved it from an excel sheet to a .csv file. I am trying to use C++ to read in a .csv file and output the first two column values in the .csv file into a text file based on the last three column values. I am assuming the .csv file looks like

1,1,value,value,value

1,2,value,value,value

1,3,value,value,value

But I couldn't find a whole lot of documentation on the format of .csv files.

I looked at Reading Values from fields in a .csv file? and used some of the code from there.

Here is my code:

#include <iostream>

#include <fstream>

using namespace std;

char separator;
int test_var;

struct Spaxel {
  int array1;
  int array2;
  int red;
  int blue_o2;
  int blue_o3;
};

Spaxel whole_list [3];

int main()
{
    // Reading in the file
    ifstream myfile("sample.csv");
    Spaxel data;
    int n = 0;
    cout << data.array1<< endl;
    myfile >> data.array1; // using as a test to see if it is working
    cout << data.array1<< endl;
    while (myfile >> data.array1)
    {
        // Storing the 5 variable and getting rid of commas
        cout<<"here?"<< endl;
        // Skip the separator, e.g. comma (',')
        myfile >> separator;

        // Read in next value.
        myfile >> data.array2;

        // Skip the separator
        myfile >> separator;

        // Read in next value.
        myfile >> data.red;

        // Skip the separator, e.g. comma (',')
        myfile >> separator;

        // Read in next value.
        myfile >> data.blue_o2;

        // Skip the separator
        myfile >> separator;

        // Read in next value.
        myfile >> data.blue_o3;

        // Ignore the newline, as it is still in the buffer.
        myfile.ignore(10000, '\n');

        // Storing values in an array to be printed out later into another file
        whole_list[n] = data;
        cout << whole_list[n].red << endl;
        n++;

        }
    myfile.close();

    // Putting contents of whole_list in an output file
    //whole_list[0].red = whole_list[0].array1 = whole_list[0].array2 = 1; this was a test     and it didn't work
    ofstream output("sample_out.txt");
    for (int n=0; n<3; n++) {
        if (whole_list[n].red == 1)
            output << whole_list[n].array1 <<","<< whole_list[n].array2<< endl;
    }
    return 0;
}

When I run it in Xcode it prints three 0's (from the cout << data.array1<< endl; and cout << data.array1<< endl; in the beginning of the main() and from the return 0) but does not output any file. Apparently the .csv file isn't getting read in correctly and the output file is not getting written correctly. Any suggestions?

Thanks for your time!

解决方案

There are a couple of problem areas in the code you presented:

  • Hard coded filename. Running your program in a directory that doesn't have "sample.csv" could cause the ifstream failure you're seeing.
  • No checking whether myfile opened successfully or not.
  • Loop can access an out-of-bound index in whole_list if "sample.csv" has more lines.

The refactored code below, while not completely foolproof, corrects many of the issues mentioned. It should get you most of the way there.

#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;


struct Spaxel
{
  int array1;
  int array2;
  int red;
  int blue_o2;
  int blue_o3;
};

ostream& operator << (ostream &os, const Spaxel &rhs)
{
  os << rhs.array1
     << ','
     << rhs.array2
     << ','
     << rhs.red
     << ','
     << rhs.blue_o2
     << ','
     << rhs.blue_o3;

  return os;
}

istream& operator >> (istream &is, Spaxel &rhs)
{
  char delim;
  is >> rhs.array1
     >> delim
     >> rhs.array2
     >> delim
     >> rhs.red
     >> delim
     >> rhs.blue_o2
     >> delim
     >> rhs.blue_o3;

  return is;
}


int main(int argc, const char *argv[])
{
    if(argc < 2)
    {
      cout << "Usage: " << argv[0] << " filename\n";
      return 1;
    }

    const char *infilename = argv[argc - 1];
    // Reading in the file
    ifstream myfile(infilename);
    if(!myfile) 
    {
      cerr << "Couldn't open file " << infilename;
      return 1;
    }

    vector<Spaxel> whole_list;
    string line;
    while( getline(myfile, line) )
    {
      Spaxel data;
      stringstream linestr (line);
      linestr >> data;
      whole_list.push_back(data);

      cout << data << '\n';
    }
}

Edit: Just to help clarify some things from the comment.

As you know main is the entry point of your program so it isn't something called by your own code. The extra optional parameters int argc, const char *argv[], is how options and parameters get passed in when you run your program with arguments. First parameter argc indicates how many arguments were passed in. The second argv is an array of char * with each element being the argument passed. The first argument argv[0] is your program name and so argc is always >= 1.

Say you execute your sample program from the shell:

./sample sample.csv

then argc and argv will have the following:

argc = 2;
argv[0] = "sample"
argv[1] = "sample.csv"

So const char *infilename = argv[argc - 1]; gets the last argument passed in which should be the filename to read in.

这篇关于如何使用C ++读取.csv文件并以另一种形式输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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