0

I.

I have a csv file that looks like this:

 XXXX,20140101
 XXXX,20140102
 XXXX,20140103
 XXXX,20140108
 XXXX,20140212

and so on, it's much larger than just that.

II.

The following method call will start the process I want to design:

 Calendar XXXXCal = FromFile("h:\\temp\\XXXXdata.csv","XXXX");

III.

Using C++ I would like to read take those input parameters, which consists of a file and a specific value. The following code illustrates to some degree what I'm trying to do, first, take a look at it, and below I'll further clarify my intent.

BespokeCalendar Calendar::XXXXCal(string input_file_name, string input_market)
{
    /* Declare the variables to be used */
    BespokeCalendar result;
    string filename = input_file_name;
    string marekt = input_market;

    string csvLine
    string csvMarket;
    string csvDate; 

    ifstream csvFile ( filename ); // declare file stream
    if( csvFile.good()) )
    {
        while (getline (csvFile,csvLine)
        {
            var containerArray = line.Split(",");
            var csvDate = containerArray[1];
            var csvMarket = containerArray[0];

            if (csvMarket == marekt)
            {
                result.addHoliday( csvDate );
            }
        }
    }
    else 
    {
        cout << "Unable to open file";
    }

    result.addWeekend();
    return result;

}

So as you can see I want to read from the csv file, line by line, something which the above code certainly does not accomplish. I've read that the ifstream handles iteration over the file, and that the getline will get me to the next line, is it so? How does that work?

I'm also doubtful that the split method exists like that, do I have to construct it out of "find_first_of" and "substr"?

How can I write a reasonable test script for this?

I'm modifying the calendar class of QuantLib.

  • There's no split method for `std::string`. You can create `stringstream` of the read line, and than use `getline` again choosing `,` as delimiter to separate columns – prajmus Aug 07 '14 at 10:04
  • could you show me an example or point me in the direction of some documentation I really don't know much about C++ – user3917695 Aug 07 '14 at 10:06

2 Answers2

0

As I mentioned in a comment, you should create a stringstream of the line that you read and then split it by the comma character using getline on the stringstream object. It's much easier than creating a split function for strings.

ifstream csvFile ( filename ); // declare file stream
if( csvFile.good()) )
{
    while (getline (csvFile,csvLine)
    {
        stringstream ss(csvLine);
        string csvMarket;
        string csvDate;

        getline(ss, csvMarket, ',');
        getline(ss, csvDate, ',');

        if (csvMarket == marekt)
        {
            result.addHoliday( csvDate );
        }
    }
}

P.S. var is not C++ keyword

prajmus
  • 3,171
  • 3
  • 31
  • 41
  • conceptually I think I get whats going on here, but since the data is stored in the csv like this "XXXX,20140101" should line 11 read getline(ss, csvDate, '\n'); something like that, using the '\n' instead of the ',' – user3917695 Aug 07 '14 at 10:58
  • If there's no comma it reads to the end of line and writes that into the second argument. – prajmus Aug 07 '14 at 11:03
  • whats the best way to test this out, what do you think about dev-c++? – user3917695 Aug 07 '14 at 12:04
  • @user3917695 every C++ IDE with compiler should be good, because this code uses only standard libraries – prajmus Aug 07 '14 at 12:42
  • I tried that solution but in fact when I printed out the values of the variables they were empty. I tried to demonstrate in an edit to your original comment but according to a message I received it first must be peer reviewed so I don't acutally know if you are able to see it. – user3917695 Aug 09 '14 at 14:42
  • @user3917695 I've seen it. You are printing it before reading. Put those two `getline` lines before `cout` ones. Also like I said, you don't need to change `,` to `\n` to read the csvDate – prajmus Aug 09 '14 at 14:49
  • ah, ok. I made a test case though and it crashed. i'm sorry if i'm coming across as quite dense, c++ seems to byzantine! – user3917695 Aug 09 '14 at 15:03
0
ifstream csvFile ( filename ); // declare file stream
if( csvFile.good()) )
{
    while (getline (csvFile,csvLine)
    {
        replace(csvLine.begin(), csvLine.end(), ',', ' '); // from <algorithm>
        istringstream ss(csvLine);
        istream_iterator<string> it(ss), itend; // from <iterator>
        vector<string> containerArray(it, itend); // from <vector>

        string csvDate = containerArray[1];
        string csvMarket = containerArray[0];

        if (csvMarket == marekt)
        {
            result.addHoliday( csvDate );
        }
    }
}
Marc Andreson
  • 3,405
  • 5
  • 35
  • 51