0

For an assignment I have to delete a line from a text file that has a list of directories. The user inputs the file name and the directory including the file name has to be deleted. For the assignment I have to use char arrays. I am having a problem where strcmp returns -13 when it should return 0, but this only occurs when there is another line after the one that should be deleted. Here the code in question:

void deleteSong()
{
    char userSongName[ENTRY_SZ], objectSongName[ENTRY_SZ];
    cout << "Enter the name of a song you want to delete.\n";
    cin.getline(userSongName, ENTRY_SZ);
    Node* tempNode = musicList.GetFirstNode();
    for (int n = 0; n < musicList.GetListLength(); n++)
    {
        strncpy(objectSongName, static_cast<Song*>(tempNode->data_)->GetSongName(), ENTRY_SZ);
        cout << strcmp(userSongName, objectSongName) << endl;
        if (!strcmp(userSongName, objectSongName))
        {
            ifstream songFileDir;
            ofstream tempFileDir;
            songFileDir.open(Song::songListFile_);
            tempFileDir.open("temp.txt");
            while (songFileDir.getline(userSongName, ENTRY_SZ))
            {
                if (!userSongName[0] == '\0')
                {
                    if (strcmp(strrchr(userSongName, '\\') + 1, objectSongName))
                    {
                        tempFileDir << userSongName << endl;
                    }
                }
            }
            songFileDir.close();
            songFileDir.clear(ios_base::goodbit);
            tempFileDir.close();
            tempFileDir.clear(ios_base::goodbit);
            remove(Song::songListFile_);
            rename("temp.txt", Song::songListFile_);
            musicList.RemoveThisLink(tempNode); //This calls a function that removes the node from the linked list.
            delete tempNode;
            return;
        }
        tempNode = tempNode->next_;
    }
    cout << "Song was not found.\n";
    return;
}
Mohit Jain
  • 29,414
  • 8
  • 65
  • 93
spartin503
  • 13
  • 5
  • Did you try to debug using prints or any debugger? – Mohit Jain Feb 25 '16 at 07:32
  • I have tried debugging by having it output both things it is comparing, and to the user they look like they should be the same, but to the program they aren't. – spartin503 Feb 25 '16 at 07:35
  • Print the result of `strlen`. I am sure the length is different. – Mohit Jain Feb 25 '16 at 07:44
  • I have a couple of observations: you should use std:string; you should use stricmp. – zdf Feb 25 '16 at 07:51
  • 1
    @spartin503 simple tip: if printing out strings for debugging, _always_ enclose them in quotes so you can see the whitespace! (So something like `cerr << "'" << variable << "'" << endl;`) Make this a habit and save yourself hours of pain... – AAT Feb 25 '16 at 10:31
  • Your code is not straight C++, it's C with streams. Use `std::string` instead of the libc `str*()` functions and you'll probably not have this problem. – Murphy Feb 25 '16 at 12:16

1 Answers1

0

It returns -13

13 is the ascii for Carriage return. It appears the second argument contains an extra CR at the end.

Solution
Trim the string by deleting (or replacing character with '\0') all the trailing whitespace ("ctype.h", isspace) character of the argument string.

Community
  • 1
  • 1
Mohit Jain
  • 29,414
  • 8
  • 65
  • 93