2016-02-25 31 views
0

對於任務,我必須從具有目錄列表的文本文件中刪除一行。用戶輸入文件名,包括文件名的目錄必須被刪除。對於賦值,我必須使用char數組。我有一個問題,strcmp在返回0時返回-13,但只有在應該刪除的行之後有另一行時纔會發生這種情況。在這裏,問題的代碼:Strcmp用戶輸入和文件輸入不匹配

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; 
} 
+0

您是否嘗試使用打印或任何調試器進行調試? –

+0

我已經嘗試通過讓它輸出它比較的東西來進行調試,而且對於用戶來說他們看起來應該是相同的,但是對於他們不是的程序。 – spartin503

+0

打印'strlen'的結果。我相信這個長度是不同的。 –

回答

0

它返回-13

13是回車的ASCII碼。 看來第二個參數在最後包含一個額外的CR。


Trim字符串刪去(或具有'\0'替換字符)中的所有尾隨空白(「文件ctype.h」,isspace爲)參數字符串的字符。