[英]C++ Getting input from an external file
所以我在這里有這段代碼:
std::cout << "Here's Question 2 now for " << char(156) << "200" << endl;
Sleep(2000);
PlaySound(TEXT("Millionaire/£100Play.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
std::cout << "In maths, which of these numbers is not referred to as a square number?" << endl;
Sleep(2000);
std::cout << "A: 0" << endl;
Sleep(2000);
std::cout << "B: 1" << endl;
Sleep(2000);
std::cout << "C: 2" << endl;
Sleep(2000);
std::cout << "D: 4" << endl;
Sleep(2000);
answerQues2:
std::cout << "So, A, B, C or D?";
std::cin >> answer2;
if (answer2 == "C" || answer2 == "c")
{
std::cout << "That's correct, you've won " << char(156) << "200!" << endl;
PlaySound(TEXT("Millionaire/£100correct.wav"), NULL, SND_FILENAME);
Sleep(2000);
}
現在,代碼本身不是問題。 這本質上是一個帶問題的測驗,然后是4個答案(A,B,C和D)。 現在,為了真正解決更多問題,您必須進入代碼本身,並經過一個漫長的過程來編輯所有內容。 我想制作一個文本文件,以便您可以編輯文本文件中的問題和答案,從而替換代碼中的所有內容(例如,如果我想更改Q1,則可以打開文本文件,替換問題並在何時替換我加載了程序,問題將被更改)。 我將如何做到這一點?
這是一個完整的解決方案,盡管您必須填寫其余的現有代碼。 我個人使用下面的函數GetFileLines將行從文件加載到向量中。 用這種方式容易工作。 我花了一些時間將其調整為char / string,因為這是您正在使用的,盡管我默認為wstring / wchar_t。
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <Windows.h>
using namespace std;
bool FileExists(const std::string& name) {
FILE * file;
errno_t result = fopen_s(&file, name.c_str(), "r");
if (result == static_cast<errno_t>(0)) {
fclose(file);
return true;
}
else {
return false;
}
}
std::vector<std::string> GetFileLines(std::string filePath)
{
vector<string> lines;
if (!FileExists(filePath))
return lines;
ifstream input(filePath);
if (!input.is_open() || input.fail())
return lines;
string line;
do {
std::getline(input, line);
lines.push_back(line);
} while (!input.eof() && !input.fail() && !input.bad());
if (!input.eof() && (input.fail() || input.bad()))
throw exception("GetFileLines failure");
return lines;
}
int wmain() {
vector<string> quizLines = GetFileLines("c:\\quiz.txt"); // replace with path to your file
if (quizLines.size() == 5) {
string question = quizLines[0];
string answer1 = quizLines[1];
string answer2 = quizLines[2];
string answer3 = quizLines[2];
string answer4 = quizLines[2];
// Your code begins here
std::cout << "Here's Question 2 now for " << char(156) << "200" << endl;
Sleep(2000);
PlaySound(TEXT("Millionaire/£100Play.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
std::cout << question << endl;
Sleep(2000);
std::cout << "A: " << answer1 << endl;
// Rest of your code with changes to use answer# variables should follow
}
else {
std::cout << "Could not load quiz from external file. Cannot continue." << endl;
}
}
我建議您閱讀一些您不熟悉的關於此標准庫元素的文檔。 這些鏈接中的任何一個(按最常用的順序排列)可能對您有用:
http://www.cplusplus.com/reference/string/string/
http://www.cplusplus.com/reference/vector/vector/
http://www.cplusplus.com/reference/fstream/ifstream/
並且不要注意人們低估一個誠實的問題。 有些人出生在這個世界上,似乎在倒立。
而且,根據記錄,這是一個非常容易的問題。 為什么? 不是因為這是一個愚蠢的問題,而是因為想像一下嘗試訪問文件內容必須多么普遍。 因此,如果您問一個基本問題,例如我如何獲得該文件內容,則應該期望有很多快速的完整答案,因為像我這樣,它們應該在手邊。 當然,可以通過在線搜索找到答案,盡管找出要閱讀其文檔的文章並不總是那么容易。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.