【问题标题】:C++ Getting input from an external fileC++ 从外部文件获取输入
【发布时间】:2014-10-05 20:21:41
【问题描述】:

所以我这里有这段代码:

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,我可以打开文本文件,替换问题以及何时我加载程序,问题会改变)。我怎么能做到这一点?

【问题讨论】:

  • “我怎么能做到这一点?” - 编写代码。如果您迷失了任何一点,请提出问题。但一如既往,首先谷歌。
  • @KarolyHorvath 当我说“我怎么能做到这一点”时,我的意思是我必须在代码方面做什么?
  • 抽象(class 表示问题)、存储容器(list&lt;Question&gt;)、文件处理(ifstream::open、文件解析)...
  • 当然还要决定你将使用什么文件格式。
  • 第一步,您可以创建一个类似于bool AskQuestion(int Nr, int Score, string Question, string AnswerA, string AnswerB, string AnswerC, string AnswerD, char CorrectAnswer); 的函数,然后通过减少参数数量来改进它。

标签: c++ file function text external


【解决方案1】:

这是一个完整的解决方案,但您必须填写现有代码的其余部分。我个人使用以下函数 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/

不要关注人们对诚实问题的评价。有些人出生在这个世界上似乎是倒立。

而且,为了记录,这是一个非常〜容易〜回答的问题。为什么?不是因为这是一个愚蠢的问题,而是因为想象一下尝试访问文件内容是多么普遍。因此,如果您问一个基本问题,例如我如何获取该文件内容,您应该期待很多快速完整的答案,因为就像我的情况一样,它们应该在手边。当然,可以通过在线搜索找出它,尽管找出你应该阅读哪些文档并不总是那么容易。

【讨论】:

  • 我们需要更多像你这样的人,我的朋友。这正是我所需要的,非常感谢!
  • 很好的恭维 =) 另外,我怀疑这在您的用例中并不重要,但在文档中找到更好的方法后,我更新了 GetFileLines 函数以不使用静态大小的缓冲区。跨度>
  • 另外,如果您单击我的答案旁边的复选标记以接受它作为最佳答案,它将提高我和您的评分 =)
  • 感谢您的更新,我点击了复选标记:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 2013-05-24
  • 2012-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多