Program: Reading CSV data file into a 2D array



STOP PRESS! The full book is now available DIRECTLY from Amazon with lower shipping costs!


I was asked to solve this problem by another teacher. Here goes...

How do you read in a file of CSV data?




The file in question looks like this..


Monday,Maths,Computing,Science,PE,German
Tuesday,French,PE,Computing,Science,Geography
Wednesday,Maths,Computing,English,PE,German
Thursday,French,PE,Computing,Science,Geography
Friday,French,English,Computing,Science,Geography



Notice there is a single comma after each text value, UNLESS it is the final text value on a row. The final value is followed by an invisible "newline" character to tell the computer that the next data item is at the start of a new line of text in the file.





Select and copy the C++ source-code at the bottom of this post.

Paste into a text editor, such as Nano or Geany.


Then save the new file, ending in .cpp


I used readcsv.cpp


To compile from the command-line:


    g++ -o readcsv.cpp readcsv


To run from the command-line:


    ./readcsv


Once the program is running, you can search for the lessons on a particular day. The user is asked to type in the name of a day. This name is used to search through the 1D days array using a simple linear search. If the day is found, the index number for that day is used to display the lessons in the 2D timetable array.


Code is below...



#include <iostream>
#include <fstream>



using namespace std;


int main()
{
    const char TAB = '\t';


    string days[5];  // Name of each day
    string timetable[5][5]; // Actual lessons

    ifstream dataFile;
    dataFile.open( "timetable.csv" );


    if ( dataFile.good() )
    {
        for ( int dayNum = 0; dayNum < 5; dayNum++ )
        {

            getline( dataFile, days[dayNum], ',' );  // First item in line is name of day.


            // Now read in the five lessons for the current day...
            char delim;
            for ( int lessonNum = 0; lessonNum < 5; lessonNum++ )
            {
                if ( lessonNum < 4 )
                    delim = ',';
                else
                    delim = '\n';


                getline( dataFile, timetable[dayNum][lessonNum], delim );  // First item in line is name of day.


            }  // end of for loop for lessonNum

        }  // end of for loop for dayNum


        // Have finished reading in the data so close the file
        dataFile.close();


        // Search for particular day
        cout << "Enter the day you want to see lessons for: ";

        string targetDay;
        cin >> targetDay;

        cout << endl;


        // Match targetDay to one of the day names to find row number in the array
        int index = 0;
        bool found = false;
        while ( not found and index < 5)
            if ( targetDay == days[index] )
                found = true;
            else
                index++;



        // Display results of search
        if ( found )
        {
            cout << "You have these lessons:" << endl << TAB;


            for ( int lessonNum = 0; lessonNum < 5; lessonNum++ )
            {
                cout << timetable[index][lessonNum];


                if ( lessonNum < 4 )
                    cout << ", ";
            }  // end of for loop that displays lessons


            cout << endl << endl;
        }
        else
            cout << "Don't be silly." << endl;



        return 0;  // Error code to indicate all was fine
    }
    else
        return 13;
  // Could not find and open file
}



Find out more - buy the book on Amazon...




Further details in Chapter 9: Arrays of data and Chapter 11: Files of data