0

I am trying to write a very basic program to read in a CSV and display it. The function, as you can see below, is a very basic one. It uses getline to read in each row (until the newline chracter) before displaying each cell. It was working fine and I was finetuning the loop to display each cell when getline simply stopped working. Without changing any of the code to do with getline, I compiled it and it would not read from the file. row is always empty. However, the ifstream is valid because the "test" string (which I inserted in response) reads in from sheet fine. Can anyone help me?

PS: I have had similar problems with getline before. Is it something to do with my system? It seems to work on and off

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
ifstream sheet( "StockTake.csv", ios::app );
if ( ! sheet.is_open() )
{
    cout << "Cannot Open File!\n";
    return 0;
}
else
{
    cout << "Opened File\n";
}
//Now print each row (find the newline chracter at the end of each line)
/*string test;
sheet >> test;
cout << test;*/
for ( string row; ! sheet.eof(); getline(sheet, row, '\n') )
{
    if (row == "")
    {
        cout << "Bad Read.  Exiting\n";
        return 0;
    }
    //Print out each row with a tab space between each cell
    string cell;
    //find the beginning and the end of each cell
    for (int i = 0, j = 0; ; )
    {
        /*Checks if the cell is enclosed in quotes
        The first time, j == i hence the -2 and +2
        the +2 is required to "skip" out the apostrophes if
        they are found*/
        if (row.at(i) == '"')
        {
            i++;
            j = i-2;
            do
            {
                j = row.find('"', j+2);
            } while (row.at(j+1) == '"');
            /*Check for the "" apostrophe sign*/
        }
        else
        {
            j = row.find(',', i);
        }
        /*Print the Cell*/
        if (j == string::npos) //if this is the last cell
        {
            cell = row.substr(i, row.size() - i);
        }
        else if ( j-i != 0)
        {
            cell = row.substr(i, j-i);
        }
        else
        {
            cell = "";
        }
        /*Check for the "" apostrophe sign, and replace
        with " for each instance*/
        if ( cell.find("\"\"", 0) != string::npos )
        {
            int pos = -2; //must start at zero
            do
            {
                //Skips out the "current" apostrophe
                //if there are more than one
                pos = cell.find("\"\"", pos+2);
                cell = cell.substr(0, pos) + 
                       cell.substr(pos+1, cell.size()- (pos+1) );
            } while (cell.find("\"\"", pos+2) != string::npos);

        }
        /*Display the Cell, only the space will be displayed
        if the cell is empty*/
        cout << cell << "     ";
        /*Find the next cell*/
        if ( row.at(j) == '"' )
        {
            i = j+2;
        }
        else
        {
            i = j+1;
        }

    }
    cout << "\n"; //Print newline character at the end of each line
}
return 0;
}
Jeremy Morren
  • 346
  • 4
  • 13
  • 2
    I don't know if its the root cause of your pain, but you shouldn't check for `eof` as your terminating condition. See: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Chad Jul 20 '16 at 14:41
  • Well, maybe that is so, but that isn't the cause of the problem. It enters the loop, and then prints "Bad Read Exiting" Every time. And it always opens the file...but Thanks for the tip. – Jeremy Morren Jul 20 '16 at 14:46
  • 1
    Just looking at your code I'd expect that for loop to never work since row will be empty on the first iteration. – Retired Ninja Jul 20 '16 at 14:46
  • @RetiredNinja has it right, I think. The `getline()` will only execute after the first loop iteration, so the first time through `row` is blank. – Chad Jul 20 '16 at 15:23
  • 1
    Why are you opening an input stream for appending? – Galik Jul 20 '16 at 15:37
  • Yes, that was the problem. I don't really understand why that code did execute (and it did). The compiler must suddenly have said: hey, I've had enough of doing it wrong. Chad, in answer to your question, I have no experience at all and I am doing this to practice. I think you can see why. Thanks a lot for the help. – Jeremy Morren Jul 20 '16 at 15:52

0 Answers0