[英]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.