1 year ago

#261447

test-img

Keean Ferreira

Reading from CSV file in C++, while (fin >> temp) never triggers

I am currently busy with a project in which I am creating a playlist that stores music, and their respective metadata (name, song duration, file type etc). I'm trying to store the data on a csv file. To acomplish this, I'm trying to create a "create" function that creates the file (or opens it if it alread exists) and adds the song data, and a "read_record" function that takes input of what it would like to access in the csv (song number and data - like name or duration) and then return the value.

I run into an issue when the reading the CSV. The following code is taken from the create function:

void PlaylistComponent::create(Array<File> tempList)
{
// file pointer
std::fstream fout;

// opens an existing csv file or creates a new file.
fout.open("playlist.csv", std::ios::out | std::ios::app);

String fileName, fileType, filePath, fileDuration;

for (int i = 0; i < tempList.size(); i++)
{
    trackTitles.push_back(tempList[i].getFileName());
    trackList.push_back(tempList[i]);

    tableComponent.updateContent();
    DBG("pushed");
    
    roll += 1;
    std::string rollString = std::to_string(roll);
    
    fileName = tempList[i].getFileName().toStdString();
    filePath = URL{tempList[i]}.toString(false).toStdString();
    fileDuration = std::to_string(trackDuration( URL { tempList[i] }));
    fileType = tempList[i].getFileExtension().toStdString();
    
    // Insert the data to file
    fout << rollString << ","
         << fileName << ","
         << filePath << ","
         << fileDuration << ","
         << fileType
         << "\n";
    
    DBG("roll: " << rollString << " filename: " << fileName << " filepath: " <<  filePath << " file duration: " << fileDuration << " filetype : " << fileType);
}

}

and the following from the read_record function:

String PlaylistComponent::read_record(std::string action, int trackID)
{
// File pointer
std::fstream fin;

// Open an existing file
fin.open("playlist.csv", std::ios::in);

int roll2, count = 0;
String result;
trackID += 1;

// Read the Data from the file
// as String Vector
std::vector<std::string> row;
std::string line, word, temp;

while (fin >> temp)
{
    DBG("clearing row");
    row.clear();

    // read an entire row and
    // store it in a string variable 'line'
    std::getline(fin, line);
    DBG("Getting line : " << line);

    // used for breaking words
    std::stringstream s(line);

    // read every column data of a row and
    // store it in a string variable, 'word'
    while (std::getline(s, word, ','))
    {
        // add all the column data
        // of a row to a vector
        row.push_back(word);
        DBG("adding word: " << word);
    }
    for (int i = 0; i < row.size(); i++)
    {
        DBG(i << " is " << row[i]);
    }

    // convert string to integer for comparision
    roll2 = stoi(row[0]);

    // Compare the roll number
    if (roll2 == trackID)
    {
        if (action == "name")
        {
            count = 1;
            DBG("file name: " << row[1]);
            result = row[1];
            break;
        }
        else if (action == "path")
        {
            count = 1;
            DBG("file path: " << row[2]);
            result = row[2];
            break;
        }
        else if (action == "duration")
        {
            count = 1;
            DBG("file duration: " << row[3]);
            result = row[3];
            break;
        }
        else if (action == "type")
        {
            count = 1;
            DBG("file type: " << row[4]);
            result = row[4];
            break;
        }
    }
}
if (count == 0)
{
    std::cout << "Record not found\n";
    result = "NaN";
}
return result; }

I notice that the issue comes in where while (fin >> temp) never triggers in the read function, so it always returns NaN. I've read that if this condition thinks that the file has no more legitimate data, then it won't pass, so is that my problem? Is my data illegitimate? If so then is there a solution for the problem?

Edit:

To solve the problem:

  1. I went into the settings, Product -> Scheme -> Edit Scheme -> Run -> Options, and then allowed for custom working directories to be used.
  2. In the read_record function, I dropped the while(fin >> temp) condition and conducted everything within an if(fin.is_open()) to ensure that the file was being opened properly.
  3. I used a vector<vector> to store the words in rows and the rows together.

c++

while-loop

opencsv

read.csv

0 Answers

Your Answer

Accepted video resources