2014-10-05 60 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