我試圖去通過與它的彙編指令一個簡單的文本文件,它看起來像這樣解析示例文本文件和拆分它
TOP NOP
VAL INT 0
TAN LA 2,1
這只是一個小例子,所以我可以證明你是如何工作的。基本上,我將第一個標籤放在標籤中,然後是第二個標籤,它們是NOP,INT和LA,並將它們放入操作碼中。
之後,我將第一個參數(0和2)並將它們放在arg1中。但這裏是我的問題的用武之地。隨着當前代碼我有,輸出我得到的,當我把參數到字符串是這樣
TOP
0
2
很顯然,我想只得到最後兩是唯一的,但我怎樣才能讓TOP不會被我的第一個參數拋在那裏呢?
#include <string>
#include <iostream>
#include <cstdlib>
#include <string.h>
#include <fstream>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[])
{
// If no extra file is provided then exit the program with error message
if (argc <= 1)
{
cout << "Correct Usage: " << argv[0] << " <Filename>" << endl;
exit (1);
}
// Array to hold the registers and initialize them all to zero
int registers [] = {0,0,0,0,0,0,0,0};
string memory [16000];
string Symtablelab[1000];
int Symtablepos[1000];
string line;
string label;
string opcode;
string arg1;
string arg2;
// Open the file that was input on the command line
ifstream myFile;
myFile.open(argv[1]);
if (!myFile.is_open())
{
cerr << "Cannot open the file." << endl;
}
int counter = 0;
int i = 0;
int j = 0;
while (getline(myFile, line, '\n'))
{
if (line[0] == '#')
{
continue;
}
if (line.length() == 0)
{
continue;
}
if (line[0] != '\t' && line[0] != ' ')
{
string delimeters = "\t ";
int current;
int next = -1;
current = next + 1;
next = line.find_first_of(delimeters, current);
label = line.substr(current, next - current);
Symtablelab[i] = label;
current = next + 1;
next = line.find_first_of(delimeters, current);
opcode = line.substr(current, next - current);
if (opcode != "WORDS" && opcode != "INT")
{
counter += 3;
}
if (opcode == "INT")
{
counter++;
}
delimeters = ", \n\t";
current = next + 1;
next = line.find_first_of(delimeters, current);
arg1 = line.substr(current, next-current);
cout << arg1<<endl;
i++;
}
}
零長度線的檢查應循環內的第一個檢查。當你還不知道它是否存在時,你正在閱讀'line [0]' – jrok
感謝你。我改變了這一點。你有什麼想法如何解決我遇到的問題。 :) – cadavid4j