我用C++编程,我不知道如何实现以下功能:
我正在将文件流复制到内存中(因为我被要求,我更喜欢从流中读取),然后尝试访问其值以将它们存储到字符串和int变量中.
这是为了创建一个解释器.我将尝试解释的代码是(即):
10 PRINT A
20 GOTO 10
Run Code Online (Sandbox Code Playgroud)
这只是一个快速示例代码.现在,值将首先存储在"地图"结构中,并在以后将"解释"所有内容时进行访问.要存储的值是:
int lnum //行号
string cmd //命令(PRINT和GOTO)
string exp // expression(在这种情况下为A和10但可以保存像(a*b)-c这样的表达式)
问题给出了以下代码,如何访问这些值并将它们存储在内存中?exp字符串也是可变大小的(可以只是一个变量或一个表达式)所以我不知道如何读取它并将其存储在字符串中.
码:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cstring>
#include <map>
#include <sstream>
using namespace std;
#include "main.hh"
int main ()
{
int lenght;
char *buffer;
// get file directory
string dir;
cout << "Please drag and drop here the file to interpret: ";
getline (cin,dir);
cout << "Thank you.\n";
cout << "Please wait while your file …Run Code Online (Sandbox Code Playgroud)