title: How to use C++ to read in a .csv file and output in another form?
body: <p>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</p>
  
  <p>1,1,value,value,value</p>
  
  <p>1,2,value,value,value</p>
  
  <p>1,3,value,value,value</p>
  
  <p>But I couldn't find a whole lot of documentation on the format of .csv files.</p>
  
  <p>I looked at <a href="https://stackoverflow.com/questions/6943829/reading-values-from-fields-in-a-csv-file">Reading Values from fields in a .csv file?</a> and used some of the code from there.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  #include &lt;fstream&gt;
  
  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 &lt;&lt; data.array1&lt;&lt; endl;
      myfile &gt;&gt; data.array1; // using as a test to see if it is working
      cout &lt;&lt; data.array1&lt;&lt; endl;
      while (myfile &gt;&gt; data.array1)
      {
          // Storing the 5 variable and getting rid of commas
          cout&lt;&lt;"here?"&lt;&lt; endl;
          // Skip the separator, e.g. comma (',')
          myfile &gt;&gt; separator;
  
          // Read in next value.
          myfile &gt;&gt; data.array2;
  
          // Skip the separator
          myfile &gt;&gt; separator;
  
          // Read in next value.
          myfile &gt;&gt; data.red;
  
          // Skip the separator, e.g. comma (',')
          myfile &gt;&gt; separator;
  
          // Read in next value.
          myfile &gt;&gt; data.blue_o2;
  
          // Skip the separator
          myfile &gt;&gt; separator;
  
          // Read in next value.
          myfile &gt;&gt; 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 &lt;&lt; whole_list[n].red &lt;&lt; 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&lt;3; n++) {
          if (whole_list[n].red == 1)
              output &lt;&lt; whole_list[n].array1 &lt;&lt;","&lt;&lt; whole_list[n].array2&lt;&lt; endl;
      }
      return 0;
  }
  </code></pre>
  
  <p>When I run it in Xcode it prints three 0's (from the cout &lt;&lt; data.array1&lt;&lt; endl; and cout &lt;&lt; data.array1&lt;&lt; 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?</p>
  
  <p>Thanks for your time!</p>