2013-04-09 77 views
0

我開發了一個腳本語言的框架工作,我正在嘗試編譯器爲它輸出一個自定義的十六進制代碼,以供應用程序(如遊戲和電影)的解釋器讀取。解釋器將讀取十六進制並通過循環執行操作,並且幾乎爲我做了艱苦的工作。但我需要一種方法將文本字符串編譯成我自定義的十六進制。這是我編譯器的一個例子。編譯我的腳本語言

#include <iostream> 
#include <string> 
#include <Windows.h> 
#include <wincon.h> 
#include <fstream>; 
using namespace std; 

int main(){ 
    char egScriptpath[999]; 
    char path[999]; 
    char title[999]; 
    ifstream readfile; 
    ofstream myfile; 
    int optn; 

    cout << "Enter the path where your *.egSCRIPT file is: " << endl; 
    cin.getline(egScriptpath,999); 

    cout << "Enter your path where you want to compile your *.egSCRIPT file: " << endl; 
    cin.getline(path,999); 

    cout << "Enter your title for your program: "; 
    cin.getline(title,999); 

    cout << endl << "Enter for your option of file extention:" << endl; 
    cout << " Enter 1 for .egGame :" << endl; 
    cout << " Enter 2 for .egMovie: "; 
    cin >> optn; 

    if(optn==1){ 
     readfile.open((string)egScriptpath); 
     ofstream egMovieFile((string)path+"\\"+(string)title+".egGame"); 
     myfile.open((string)path+"\\"+(string)title+".egGame"); 
     while(!readfile.eof()){ 
      if(readfile.getline("validated()",1)){ 
       myfile << "    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n" 
         "    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n" 
         "    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n" 
         "    ////////////////\n" 
         "    ////////////////\n" 
         "    ||||||||||||||||\n" 
         "    ||||||||||||||||\n" 
         "    ||||||||||||||||\n" 
         "    \1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\n" 
         "    \2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\n"; 
      } 
     } 
     myfile.close(); 
     readfile.close(); 

    }else if(optn==2){ 
     readfile.open((string)egScriptpath); 
     ofstream egGameFile((string)path+"\\"+(string)title+".egMovie"); 
     myfile.open((string)path+"\\"+(string)title+".egMovie"); 

     while(!readfile.eof()){ 
      if(readfile.getline("validated()",1)){ 
      myfile << "    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n" 
         "    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n" 
         "    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n" 
         "    ////////////////\n" 
         "    ////////////////\n" 
         "    ||||||||||||||||\n" 
         "    ||||||||||||||||\n" 
         "    ||||||||||||||||\n" 
         "    \1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\n" 
         "    \2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\n"; 
      } 
     } 
     myfile.close(); 
     readfile.close(); 
    } 

    cout << "Compile complete" << endl; 
    system("pause"); 

    return 0; 
} 

所以我想要做的就是在if語句,它說如果(readfile.getline(「確認()」,1)){}我希望它寫到「MYFILE」 ofstream的變量您只能看到一個例子。我希望我明白我的意思。如果編譯器在我的腳本文件中讀取「validated()」,那麼它會將「十六進制」代碼寫入具有「myfile」變量的新文件,然後解釋器將其解釋爲遊戲和電影等應用程序。謝謝你,如果你知道什麼是錯的,爲什麼它不會將十六進制代碼寫入到它創建的新文件中。

回答

2

看起來像你的問題所在位置:

if(readfile.getline("validated()",1)){ 

真誠的,我甚至不知道爲什麼compiller允許你這樣做,但我想建議你以下變化:

而不是

while(!readfile.eof()){ 
    if(readfile.getline("validated()",1)){ 

使用這一個:

std::string buffer; 
while (std::getline(readfile, buffer, '\n')){ 
    if (buffer == "validated()") 

此外,由於您使用的是C++,而不是使用char數組,所以您應該更多地使用std :: string類。

+0

我知道這是錯誤的地方,但我無法找出它有什麼問題。你知道嗎? – 2013-04-09 01:20:23

+0

非常感謝,它的工作原理 – 2013-04-09 01:27:27