Strcmp 用户输入和文件输入不匹配
Strcmp user input and file input not matching
对于作业,我必须从包含目录列表的文本文件中删除一行。用户输入文件名,必须删除包含文件名的目录。对于作业,我必须使用 char 数组。我遇到了一个问题,strcmp 在应该返回 0 时返回 -13,但这只发生在应该删除的行之后有另一行时。这里有问题的代码:
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] == ' ')
{
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;
}
它返回 -13
13 是回车的 ASCII。第二个参数似乎在末尾包含一个额外的 CR。
解决方案
通过删除(或用 ' '
替换字符)参数字符串的所有尾随空格("ctype.h",isspace)字符来修剪字符串。
相关文章:
- Qt SQLite没有查询或参数计数不匹配
- 模板参数推导失败,函数参数/参数不匹配
- 在使用累加时,C++中的运算符+不匹配
- C++ 与操作员不匹配<<
- 打印时有二叉树问题.用户输入不打印任何内容
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 查找匹配的输入,并返回匹配的输入和不匹配的输入
- CvMat:输入参数的大小不匹配
- 输入不匹配 JNA
- 来自 txt 文件的输入在第二次读取/传递时不匹配 (C++)
- 为什么这个提升::精神::qi规则与输入不匹配
- push_back opencv中输入参数的格式不匹配
- Strcmp 用户输入和文件输入不匹配
- 为什么函数中的对象输入不匹配但仍然有效
- 输入参数的位和大小不匹配
- 与使用运算符重载 C++ 的运算符<<输入输出不匹配
- OpenCV 错误:输入参数的大小不匹配(该操作既不是"数组操作数组")
- OpenCV-输入参数的大小与-addWeighted不匹配
- 用户输入值,但打印到屏幕的任意值。打印值与输入的值不匹配
- c++文件处理-输出与输入不匹配