How to use C ++ to read in a CSV file and output in a different form?

I have a CSV file with three rows and 5 columns with values ​​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 a CSV file to a text file based on the last three column values. I suppose that. CSV file looks like

1.1, value, value, value

1,2, value, value, value

1.3, value, value, value

But I could not find a lot of documentation in CSV file format.

Have I looked at Reading Values ​​from Fields in a CSV File? and used part of the code.

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, eg 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, eg 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 0s (from cout <data.array1 <endl; and cout <data.array1 <end1; at the beginning of the main () and from return 0), but it doesn't output any file. Apparently the .csv file does not read correctly and the output file does not work correctly written. Any suggestions?

Thank you for your time!

+4
source share
1 answer

There are several problem areas in the code you submitted:

  • Hard coded file name. Running your program in a directory that does not have "sample.csv" may cause the ifstream that you see to crash.
  • It does not check if myfile opened successfully.
  • Loop can access an out-of-range index in whole_list if "sample.csv" has more rows.

The reorganized code below, although not completely reliable, fixes many of the problems mentioned. That should make you most of the way.

 #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 clarify some things from the comment.

As you know, main is the entry point of your program, so no code calls it. Additional optional parameters int argc, const char *argv[] is how parameters and parameters are passed when your program starts with arguments. The first argument to argc indicates how many arguments were passed. The second argv is an array of char * , with each element being an argument passed. The first argument to argv[0] is your program name, and therefore argc always >= 1 .

Suppose you run your sample program from a 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 the file name should be written.

+4
source

Source: https://habr.com/ru/post/1485870/


All Articles