0

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.
  • 1
    Make sure your file `is_open` before you try reading from it. – NathanOliver Mar 07 '22 at 13:01
  • @NathanOliver Will // Open an existing file fin.open("playlist.csv", std::ios::in); not do that? – Keean Ferreira Mar 07 '22 at 13:13
  • 1
    Nope. `open` will open the file if it can, but if it can't, it doesn't tell you. You need to make sure it actually opened using `is_open`. – NathanOliver Mar 07 '22 at 13:16
  • Hmm..., you use a `while (fin >> temp)` and read from `fin` in the loop. You should read [this other post](https://stackoverflow.com/q/5605125/3545273)... – Serge Ballesta Mar 07 '22 at 13:17
  • @NathanOliver I did a check, and before the *while (fin >> temp)* the file is not open. Any idea as to why that is? – Keean Ferreira Mar 07 '22 at 13:18
  • Maybe you put the file csv in the wrong folder or named it differently. Your OS will not search for a file in any folder other than the current working directory. And the file name and extension must match. Some systems have case insensitive file names but others have case sensitivity. If you are running your program from an IDE it usually sets the current working directory when you run in the IDE. For Visual Studio (not VSCode) the default working directory is the location of the project file set as $(ProjectDir) in the debugger props. Other IDEs it's usually the same place as the executable. – drescherjm Mar 07 '22 at 13:41
  • On top of that the Windows file explorer can trick you because by default it hides extensions of known file types. This can cause your file to possibly be named `playlist.csv.txt` or `playlist.csv.csv` without you knowing because of the hidden extension. I recommend turning off the option to hide the extension. If your program is looking for `playlist.csv` it won't try either of the previous file names I mentioned. It will just not find `playlist.csv` – drescherjm Mar 07 '22 at 13:43
  • After attempting, I discovered that the steps given in https://stackoverflow.com/a/23448835/12238077 allowed my files to be opened correctly. – Keean Ferreira Mar 07 '22 at 14:45

0 Answers0