2014-10-05 62 views
-1

输入所以我有这样的代码在这里:C++获取从外部文件

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

+0

“我怎么能做到这一点?” - 编写代码。如果您有任何遗失,请提出问题。但一如既往,首先谷歌。 – 2014-10-05 20:23:12

+0

@KarolyHorvath当我说“我将如何做到这一点”时,我的意思是我需要在代码方面做些什么? – SGCSam 2014-10-05 20:24:14

+1

抽象(一个问题的类),一个存储容器('列表'),文件处理('ifstream :: open',文件解析)... – 2014-10-05 20:26:55

回答

3

这是一个完整的解决方案,但您必须填写现有代码的其余部分。我个人使用下面的函数GetFileLines来将文件中的行加载到向量中。易于以这种方式工作。我花时间去适应char /字符串,因为这是你使用的,但我默认为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/

而且不注重人失望的评级诚实的问题。有些人出生在这个世界上,看起来很倒霉。

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

+1

我们需要更多的人在这个世界上像你我的朋友。这正是我所需要的,非常感谢! – SGCSam 2014-10-06 20:34:11

+1

Nice compliment =)另外,不是我怀疑它在你的用例中很重要,但是在找到更好的文档方法后,我更新了GetFileLines函数,不要使用静态大小的缓冲区。 – 2014-10-07 13:09:36

+0

此外,如果您点击我的答案的复选标记以接受它作为最佳答案,它会提高我的评分,并且您的=) – 2014-10-07 13:11:09