-1

The following program saves a list of directories to a file. The file is then read and asks the user if the folder name is a favorite or not. My problem is, the getline function does not save all the characters in the input file to a string. Here is the code:

int main()
{
    fstream list;
    fstream favorites;

    string command1;
    string command2;
    string file;
    string file2;
    string line;

    // Cycle through the directories outputing the list of directories to a file

    for (int i = 0; i < 27; i++)
    {
        system("Y:");
        command2 = "dir Y:\\Blu-Rays\\";
        command2 += letters[i];
        command2 += "\\";
        command2 += " /b > Y:\\Blu-Rays\\";
        command2 += letters[i];
        command2 += "\\files.txt";
        cout << command2 << endl;
        system(command2.c_str());

        file = "Y:\\Blu-Rays\\";
        file += letters[i];
        file += "\\files.txt";
        list.open(file);

        file2 = "Y:\\Blu-Rays\\";
        file2 += "favorites.txt";
        favorites.open(file2);

        // The dir command also lists the file.txt in the file, open the file and erase the line
        while (getline(list, line))
        {
            // If the line file.txt is recognized, do not append the line to the file
            if (line != "file.txt")
            {
                list << line << endl;
                save(line);
                line = "";
            }
        }
    }

    list.close();
    favorites.close();

    return 0;
}

Here is the output from the code:

dir Y:\Blu-Rays\#\ /b > Y:\Blu-Rays\#\files.txt
Current title: 2012
Do you want to save the title as a favorite? [y/n]: y
Current title: s to Kill
Do you want to save the title as a favorite? [y/n]: n
Current title: Rise of an Empire
Do you want to save the title as a favorite? [y/n]: n

The 4th line of the output is supposed to be 3 Days to Kill and it reads s to Kill. To follow, the fifth line is supposed to read: 300: Rise of the Empire when it reads Rise of an Empire

  • Edit your question to include a [mcve]. – Sid S Jun 09 '18 at 02:02
  • Also, the English alphabet has 26 letters, not 27. – Sid S Jun 09 '18 at 02:03
  • 1
    You are writing to the same file you are reading from. – paddy Jun 09 '18 at 02:12
  • Why are you using `system()` to enumerate the file system using a separate command process, instead of using something like `boost::filesystem`, or `File(First|Next)File()` on Windows, etc to directly enumerate the files in your own code? Then you don't need the input file at all, since you will already have the filenames in memory. – Remy Lebeau Jun 09 '18 at 03:41

1 Answers1

0

Reading and writing from the same std::fstream simultaneously probably doesn't do what you expect it to.

A simpler solution would be to use boost::filesystem or std::filesystem to iterate through the files: http://en.cppreference.com/w/cpp/experimental/fs

for(auto& path: fs::directory_iterator("y:\\blurays\\"))
        std::cout << path << '\n';
Alan Birtles
  • 22,711
  • 4
  • 22
  • 44