c++ - 使用 std::cerr 时字符被打乱
问题描述
我目前正在为学校做一个项目,无论我对输入文件或实现做什么,我都无法让它工作。
该代码在 Visual Studios 2017 中运行良好,但现在我在使用 g++ 的 Linux 环境中。
代码:
getLineFromTextFile(std::string textOrCin):
std::string getLineFromTextFile(std::string textOrCin) {
std::string currentLine;
if (textOrCin == "text") {
if (!inputStream.eof()) {
std::getline(inputStream, currentLine);
//as long as currentLine's first two digits are "//..."
while ((currentLine[0] == '/' && currentLine[1] == '/') || currentLine == "") {
std::getline(inputStream, currentLine);
}
std::cout << currentLine << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(400));
//std::this_thread::sleep_for(std::chrono::milliseconds(0));
}
else {
std::cerr << "[global::getLineFromTextFile]: Critcal Error; End of file found, can't load anymore!\n";
std::cerr << "exiting...";
throw;
}
}
else if (textOrCin == "cin") {
std::cin >> currentLine;
}
else {
std::cerr << "[global::getLineFromTextFile]: Critcal Error; Invalid 'textOrCin' input.\n";
std::cerr << "exiting...";
throw;
}
return currentLine;
}
功能实现:
void ZoinkersEngine::displayMainMenu(User& currentUser, std::string textOrCin) {
std::string option = "";
if (currentUser.getRole() == "admin") {
do {
std::cout << std::string(40, '\n');
std::cout << "Successfully logged in...\n\n\n";
std::cout << "Main Menu:\n";
std::cout << "[0] Order Plan\n";
std::cout << "[1] Generate Plan\n";
std::cout << "[2] Manage Profile\n";
std::cout << "[3] Manage Exhibits\n";
std::cout << "[4] Manage Animals\n";
std::cout << "[5] Manage Users\n";
std::cout << "[6] Search Animal/Exhibit by Name\n";
std::cout << "[7] Record Favorability\n";
std::cout << "[8] Log Animal/Exhibit Care\n";
std::cout << "[9] Add or Remove an Exhibit\n";
std::cout << "[10] Add or Remove an Animal\n";
std::cout << "[Cancel] To exit\n\n";
std::cout << "Please input number of selected option: ";
// std::cin >> option;
option = getLineFromTextFile(textOrCin);
//DEBUG START
std::cerr << "Option: [" << option "].\n";
std::cerr << "Option.size(): [" << option.size() "].\n";
//DEBUG END
if (option == "0") {
currentUser.calculateExhibitFav(zoinkersDirectory);
currentUser.orderPlan(zoinkersDirectory, textOrCin);
}
else if (option == "1") {
currentUser.calculateExhibitFav(zoinkersDirectory);
currentUser.generatePlan(zoinkersDirectory, textOrCin);
}
else if (option == "2") {
currentUser.manageProfile(zoinkersDirectory, textOrCin);
}
else if (option == "3") {
zoinkersDirectory.manageExhibit(currentUser.getUsername(), textOrCin);
}
else if (option == "4") {
zoinkersDirectory.manageAnimal(currentUser.getUsername(), textOrCin);
}
else if (option == "5") {
zoinkersDirectory.manageUsers(currentUser.getUsername(), textOrCin);
}
else if (option == "6") {
zoinkersDirectory.searchAnimalExhibit(textOrCin, currentUser);
}
else if (option == "7") {
currentUser.favorabilityUI(zoinkersDirectory, textOrCin);
}
else if (option == "8") {
currentUser.logExhibitCare(textOrCin);
}
else if (option == "9") {
zoinkersDirectory.addRemoveExhibit(currentUser.getUsername(), textOrCin);
}
else if (option == "10") {
zoinkersDirectory.addRemoveAnimal(currentUser.getUsername(), textOrCin);
}
else if (option == "cancel" || option == "Cancel") {
break;
}
} while (option != "cancel" || option != "Cancel");
}
调试输出:
Successfully logged in...
Main Menu:
[0] Order Plan
[1] Generate Plan
[2] Manage Profile
[3] Manage Exhibits
[4] Manage Animals
[5] Manage Users
[6] Search Animal/Exhibit by Name
[7] Record Favorability
[8] Log Animal/Exhibit Care
[9] Add or Remove an Exhibit
[10] Add or Remove an Animal
[Cancel] To exit
Please input number of selected option: 0
]ption: [0
option.size(): [2]
我应该只输入“0”,但正如在 'option.size(): [2]' 中看到的,还有一些额外的东西把它搞砸了。
即使它只是文本文件中的一个空行,它也会拉入那个奇怪的加扰字符(大小应该是 0 但为 1 并且仍然加扰)。
我唯一的想法是我在我的 .txt 文件中使用了一个 g++ 无法识别的奇怪的换行符。(.txt 是在崇高的文本编辑器中制作的)。
我尝试将我的代码和文本文件复制到记事本中保存、关闭、重新打开并粘贴到 emacs 中,但仍然没有运气。
注意:所有功能都适用于 VS 和 G++ 的手动输入。
任何帮助将不胜感激!
编辑1:我已经将选项输出到一个文本文件并得到了奇怪的字符,它是“^M”。
解决方案
Solved: It was exactly what I said. My test document used windows newline characters, not Unix.
Fix: Use an online converter to convert your .txt file from Windows supported characters to Unix character supported characters.
Edit #2: I used http://newline.nadav.org/
Note: I am in no way affiliated with the website I shared above!
推荐阅读
- python - 如何在不创建对象的情况下调用 python 的标准库函数?
- java - 未找到媒体类型 = 文本/xml 的 MessageBodyReader
- sql - SQL Server,测试数据库环境的每日还原
- nativescript - 如何使用 imagepicker 选择 ios 图像并在 Nativescript 中显示
- apache-spark - Pyspark 作业队列配置优先级 - spark-submit vs SparkSession.builder
- javascript - 通过 api 端点访问特定项目
- javascript - React Jsx - 如何为随机报价生成器挂钩 RxJs 点击事件?
- python - 相同数据集和超参数的差异 betweek Keras 和 Tensorflow
- reactjs - 反应内存泄漏,componentDidUnmount 不起作用
- c# - 插入数据时在 xamarin 表单中出现错误:System.ArgumentNullException:'值不能为空。参数名称:取消'