Search code examples
c++csvifstream

ifstream csv writing back from a file with commas


I'm working on this Library system in c++ for my programming class, and i cant seem to get the ifsteam working with commas, separating the sections. I made it so you can input the book title, publisher, author, ISBN, Subject, Pages, and the name you want the file to have. This all works fine. I made a function named rBook, to return the information of the book, and made it so it found the file path. The sections are separated by commas as i said but everytime i run it with this code it doesnt print everything in a list with the commas.

void rBook(){
string line, filePath, fileName;
cin >> fileName;
filePath="C:/Users/Jsadlowski/Desktop/Books/"+fileName+".txt";
ifstream myfile (filePath);
system("cls");
while ( getline (myfile,line) )
{
    int comma1 = find_Nth(line, 1, ",");
    int comma2 = find_Nth(line, 2, ",");
    int comma3 = find_Nth(line, 3, ",");
    int comma4 = find_Nth(line, 4, ",");
    int comma5 = find_Nth(line, 5, ",");

    //cout << comma1 << " " << comma2 << " " << comma3 << " " << comma4 << " " << comma5;

    string Title, Publisher, Author, ISBN, Subject,Pages;

    Title = line.substr(0,comma1);
    Publisher = line.substr(comma2 + 1,comma1);
    Author = line.substr(comma3 + 1,comma2);
    ISBN = line.substr(comma4 + 1,comma3);
    Subject = line.substr(comma5 + 1,comma4);
    Pages = line.substr(line.length(), comma5 + 1);
    cout << Title << endl;
    cout << Publisher << endl;
    cout << Author << endl;
    cout << ISBN << endl;
    cout << Subject << endl;
    cout << Pages <<  endl;

    cout << line << endl;
}
myfile.close();

This code is to return the information from the file.


Solution

  • I guess that you have a problem when using the substr function, it tooks up to 2 parameters:

    1. the position of the first character to be copied
    2. the number of characters to be copied (this is optional, if ommited it goes up to the end of the line)

    So then for example if you have the following line, those should be the values for commas 1 to 5:

         5         15     22   27      35
    title,publisher,author,ISBN,subject,pages
    

    And in code, the example will be like:

    string line = "title,publisher,author,ISBN,subject,pages";
    
    //this is what I guess is returned by your find_Nth function
    int comma1 = 5;
    int comma2 = 15;
    int comma3 = 22;
    int comma4 = 27;
    int comma5 = 35;
    
    string Title, Publisher, Author, ISBN, Subject, Pages;
    
    /**
     * using your approach will get incorrect values in some of your variables,
     * the first is ok, as it start on position 0 and then grab 5 characters,
     * the second says to start in position 16 and grab 5 characters which 
     * gets an incorrect value, and so on
     */
    Title = line.substr(0,comma1);                  //substr(0, 5)   -> title
    Publisher = line.substr(comma2 + 1,comma1);     //substr(16, 5)  -> autho 
    Author = line.substr(comma3 + 1,comma2);        //substr(23, 15) -> ISBN,subject,pa
    ISBN = line.substr(comma4 + 1,comma3);          //substr(28, 22) -> subject,pages
    Subject = line.substr(comma5 + 1,comma4);       //substr(36, 27) -> pages
    Pages = line.substr(line.length(), comma5 + 1); //substr(40, 35) -> 
    
    /**
     * A possible solution can be to change it like the following, 
     * to indicate the position of the starting comma and then 
     * calculate the length using the value of the following comma
     */
    Title = line.substr(0, comma1);                           //substr(0, 5)  -> title
    Publisher = line.substr(comma1 + 1, comma2 - comma1 - 1); //substr(6, 9)  -> publisher 
    Author = line.substr(comma2 + 1, comma3 - comma2 - 1);    //substr(16, 6) -> author
    ISBN = line.substr(comma3 + 1, comma4 - comma3 - 1);      //substr(23, 4) -> ISBN
    Subject = line.substr(comma4 + 1, comma5 - comma4 - 1);   //substr(28, 7) -> subject
    Pages = line.substr(comma5 + 1);                          //substr(36)    -> pages