I'm having problems with the code below, it compiles but no matter what I do it doesn't open the .csv file that I want it to. The file name is passed into this funtion then it must read the values in and then print them out, simple, yet I cant get it to work. I'm new to C++ so please be patient, I really need help with this!

void graph_file(const char* filename )
{
     cout << filename << endl;       //check that file name was read correctly
     ifstream newFile ;      // make an object of the input file stream class
     newFile.open(filename, ifstream::in);   // open the file
     float yVal [1025];             // float array to store incoming y-values
     int count = 0;
     if (newFile.good())                // if file was opened
     {
         while (!newFile.eof())               // read until end of file (eof)
         {
               for(int n = 0; n<1024; n++)
               {
                       newFile >> yVal [n];
                       count++;               // store floats in file to yVal array
               }
         }
         for (int z = 0; z<count; z++)         // print it if valid
         {
             cout << "READ: " << yVal[z] << endl;
         }
         newFile.close();
     }
     else cerr << "File not found.\n";
}

My particular test one is sins.csv but I need it to read many different ones depending on which one is called, but all of them will be "singleWord.csv"

Are those files in your current working directory? Try passing in a full path. Most likely the program isn't looking where you think it's looking for the specified file.

Can you use the extraction operator to pull floating-point values out of a *.csv without the commas messing up your ifstream? It seems like the stream would get corrupted by the invalid input character(s)...

Edited 6 Years Ago by Fbody: n/a

Yes the extraction operator seems to work fine with the csv because when I drag and drop the csv file onto my program, it runs fine, its just that I can't get it to open the files by typing in the file name. The csv is in the same directory, if I type in the full path name then the spaces in the path name cause problems but I guess I could change that. Where is the .open operation supposed to look for the file?

How do you read in the filename and how do you store it? It sounds like you need a different operator to read the user input. The extraction operator '>>' automatically stops extracting characters at the first whitespace (which a 'space' is) it encounters. You will probably have to switch your input method to something like istream::getline() instead.

EDIT:
>>It seems like the stream would get corrupted by the invalid input character(s)
>Yes the extraction operator seems to work fine with the csv because when I drag and drop the csv file onto my program, it runs fine
Hmmm.... Maybe the issue is implementation specific, or dependent on your o/s's regional settings. If I run this (in MS-VC++ 2008):

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
  const int VALUE_COUNT = 10;
  const int VALUE_RANGE = 10000;
  const float DECIMALS = 100.0;

  srand((unsigned)time(0));

  ofstream CSVFile("testFile.csv");

  if (CSVFile.is_open()) {
    float floatValue = 0.0;
    int randomValue = 0;

    for (int i = 0; i < VALUE_COUNT; ++i) {
      randomValue = (rand() % VALUE_RANGE);
      floatValue = randomValue / DECIMALS;
      CSVFile << floatValue << ", ";
    }
    CSVFile.close();
  } else {
    cout << "Error opening 'testFile.csv'";
    return 1;
  }

  ifstream CSVin("testFile.csv");

  if (CSVin.is_open()) {
    float inputFloat = 0.0;

    for (int i = 0; i < VALUE_COUNT; ++i) {
      CSVin >> inputFloat;
      if (!CSVin.good()) {
        cout << "input file stream CSVin is corrupt" << endl;
        CSVin.clear();
        CSVin.ignore();
      } else {
        cout << "Read value " << inputFloat << " from ifstream CSVin." << endl;
      }
    }
  } else {
    cout << "Error opening 'testFile.csv'";
    return 1;
  }

  cin.get();
  return 0;
}

It produces a file similar to this:

70.94, 46.84, 71.36, 91.18, 69.6, 56.21, 81.85, 20.89, 92.06, 79.73,

And output similar to this:

Read value 70.94 from ifstream CSVin.
input file stream CSVin is corrupt
Read value 46.84 from ifstream CSVin.
input file stream CSVin is corrupt
Read value 71.36 from ifstream CSVin.
input file stream CSVin is corrupt
Read value 91.18 from ifstream CSVin.
input file stream CSVin is corrupt
Read value 69.6 from ifstream CSVin.
input file stream CSVin is corrupt

This clearly shows my input stream getting corrupted. I'll have to experiment with some tweaks, because it seems to happen after reading a valid value. I wonder if it stops before the comma, then reads the comma on the next iteration. Because it clearly only reads half of the file.

EDIT 2:
Interesting, if I throw in these 2 lines after Line 38:

char commaCatcher = '\0';
CSVin >> commaCatcher;

it eliminates the issue. So the comma is clearly lingering, at least in my implementation. But I don't see how yours is addressing this. :confused:

Edited 6 Years Ago by Fbody: n/a

This article has been dead for over six months. Start a new discussion instead.