简体   繁体   English

C ++从外部文件获取输入

[英]C++ Getting input from an external file

So I have this code 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 << "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);
    }

Now, the code itself is not the problem. 现在,代码本身不是问题。 What this is essentially is a quiz with a question and then 4 answers (A, B, C and D). 这本质上是一个带问题的测验,然后是4个答案(A,B,C和D)。 Now in order to actually make up more questions, you'd have to go into the code itself and go through a lengthy process to edit everything. 现在,为了真正解决更多问题,您必须进入代码本身,并经过一个漫长的过程来编辑所有内容。 I want to make a text file that you can edit the questions and answers inside the text file, hence replacing everything in the code (So for example, if I wanted to change Q1, I could open the text file, replace the question and when I load up the program, the question will be changed). 我想制作一个文本文件,以便您可以编辑文本文件中的问题和答案,从而替换代码中的所有内容(例如,如果我想更改Q1,则可以打开文本文件,替换问题并在何时替换我加载了程序,问题将被更改)。 How would I be able to do this? 我将如何做到这一点?

This is a full solution, though you must fill in the rest of your existing code. 这是一个完整的解决方案,尽管您必须填写其余的现有代码。 I personally use the below function, GetFileLines, to load lines from a file into a vector. 我个人使用下面的函数GetFileLines将行从文件加载到向量中。 Easy to work with that way. 用这种方式容易工作。 I took the time to adapt it to char/string since that is what you are using, though I default to wstring/wchar_t. 我花了一些时间将其调整为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;
    }
}

I recommend you read some documentation on this standard library elements I used that you are not familiar with. 我建议您阅读一些您不熟悉的关于此标准库元素的文档。 Any of these links, ordered by most common use first, may be useful to you for that: 这些链接中的任何一个(按最常用的顺序排列)可能对您有用:

http://www.cplusplus.com/reference/string/string/ http://www.cplusplus.com/reference/string/string/

http://www.cplusplus.com/reference/vector/vector/ http://www.cplusplus.com/reference/vector/vector/

http://www.cplusplus.com/reference/fstream/ifstream/ http://www.cplusplus.com/reference/fstream/ifstream/

And don't pay attention to people down rating an honest question. 并且不要注意人们低估一个诚实的问题。 Some were born into this world doing headstands it seems. 有些人出生在这个世界上,似乎在倒立。

And also, for the record, this was an exceedingly ~easy~ question to answer. 而且,根据记录,这是一个非常容易的问题。 Why? 为什么? Not because it was a dumb question, but because imagine how common it must be to try to access file contents. 不是因为这是一个愚蠢的问题,而是因为想像一下尝试访问文件内容必须多么普遍。 So if you ask a fundamental question like how do I get at that file content, you should expect a lot of quick full answers, because like in my case, they should be on hand. 因此,如果您问一个基本问题,例如我如何获得该文件内容,则应该期望有很多快速的完整答案,因为像我这样,它们应该在手边。 Could it be figured out using online searches, of course, though it's not always easy to figure out the pieces whose documentation you should be reading. 当然,可以通过在线搜索找到答案,尽管找出要阅读其文档的文章并不总是那么容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM