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 语句中说 if(readfile.getline("validated()",1)){} 我希望它把你可以看到的文本写入流变量的“myfile”例如。我希望我清楚我在说什么。如果编译器在我的脚本文件上读取“validated()”,那么它将使用“myfile”变量将“十六进制”代码写入新文件,该变量将由解释器解释,用于游戏和电影等应用程序. 谢谢,如果您知道出了什么问题以及为什么它没有将十六进制代码写入它制作的新文件中。

4

1 回答 1

2

看起来您的问题位于此处:

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

真诚地,我什至不知道为什么编译器允许您这样做,但我想向您建议以下更改:

代替:

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

使用这个:

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

此外,由于您使用的是 C++,而不是使用 char 数组,您应该更多地依赖 std::string 类。

于 2013-04-09T01:19:14.257 回答