2012-09-16 5 views
0

나는이잘못된 출력

[라벨] 오피 [ARG1과 같이 일부 조립 설명서가 포함 된 텍스트 파일에서 읽고 있어요 ] [, arg2]

지금은 레이블을 읽는 단계에 불과하지만 파일을 읽고 배열에 레이블을 입력 할 때 몇 가지 빈 줄이 나타납니다. 그곳에. 여기 내 코드는

#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[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; 

     cout << Symtablelab[i] << endl; 

     i++; 

    } 


} 


return 0; 
} 

이 코드에서 얻을 출력은 다음과 같습니다

blank line 
TOP 
VAL 
TAN 
blank line 

난 단지 점점되어야한다 : 여기

TOP 
VAL 
TAN 

내가 읽고 있어요 샘플 텍스트 파일입니다 부터

# Sample Input 

    LA 1,3 
    LA 2,1 
TOP  NOP 
    ADDR 3,1 
    ST 3, VAL 
    CMPR 3,4 
    JNE TOP 
    P_INT 1,VAL 
    P_REGS 
    HALT 
VAL  INT 0 
TAN  LA 2,1 

답변

1

빈 줄 e 주석과 끝에있는 암시 적 빈 줄이 문제를 일으키고 있습니다. 빈 줄을 주석으로 확인한 후에 검사를 추가해야합니다.

if (line.length == 0) { 
    continue; 
} 
+0

나는 그 생각을하지 못했다고 생각하지 않습니다. 나는 내 뇌가 내가 뭘 잘못했는지 궁금해하고있다. 고맙습니다. – cadavid4j