繁体   English   中英

C ++,逐行阅读并搜索链接到文件中一行的关键字

[英]C++ , read line by line and search a keyword that links to a line in a file

我已经在可以从文件中读取指令的机器模拟器上工作了几天。 刚开始使用C ++,我正在研究向量和解析。

示例文件如下所示:

label0: left
right
if <1> goto label1
write 0
goto label2
label1: right
write 1
label2: halt

我的磁带代码如下所示:

int main() {
    int total_lines=0;
    int RWH=0;
    // Create a vector of strings
        std::vector<string> Instruction;
        // Create a string variable to hold each line read from the file.
        string InstLine;
        // Read through file and push onto our vector
    vector<char> TapeVector; //declare tape       
    char temp;
    cin >> noskipws;
    cout << "Enter the input (end with ~):"; //initialize tape
    while (cin >> temp && temp != '~') {
    TapeVector.push_back(temp);                
    }
    cout << "INPUT:";
        for (int i=0;i<TapeVector.size();++i) 
        {  
        cout << TapeVector[i];             
        }
        cout << endl;

    // Open our file
    ifstream InFile("input.txt",ifstream::in);
    // If we can read/write great
    if (InFile.good())  
    {
        {
        while(std::getline(InFile, InstLine))
             Instruction.push_back(InstLine);
             ++total_lines;
        }


    }
    else
        cout << "Error reading file";

and here is for the parsing which I'm having problems:

while (std::getline(infile, line))
    std::istringstream iss(line);
    string a, b, c, d;

    if (iss >> a >> b >> c >> d) //if 4 parameters
       {
            if (a=="if" && b==VectorTape[RWH])
            { 
                counter=0; // counter will reset if b 

            }
            ;
       }
    else if (iss >> a >> b >> ) //if 2 parameters
       {
            if (a=="goto" //unfinished
       }
    else if (iss >> a >> b) //if 2 parameters
       {
            if (a=="goto" //unfinished
       }
    else if (iss >> a) //if 1 parameter
       {

       } 



enter code here

在第3行上,如果检测到label1,如何跳过并读取带有label1的行?


#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include "label.h"
using namespace std;


int main() {

    int RWH=0;
    std::vector<string> Instruction;    
    int total_lines=0;  

    vector<char> TapeVector; //declare tape       
    char temp;
    cin >> noskipws;
    cout << "Enter the input (end with ~):"; //initialize tape

    while (cin >> temp && temp != '~') 
          {
               TapeVector.push_back(temp);                
          }
    cout << "INPUT:";

    for (int j=0;j<TapeVector.size();++j) 
          {  
               cout << TapeVector[j];             
          }
    cout << endl;

    // Open our file
    ifstream InFile("input.txt",ifstream::in);
    // If we can read/write great
    if (InFile.good())  
    {
        {        
        string InstLine;
        // Read through file and push onto our vector
        while(std::getline(InFile, InstLine))
             Instruction.push_back(InstLine);
             ++total_lines;
        }        
    }
    else
        cout << "Error reading file";

    vector<string> labels;    
    labels = lookupLabels(Instruction);
    size_t i = 0;
    while (i < Instruction.size()) 
    {
          istringstream iss(Instruction[i]);
          // ...
          string a,b,c,d;

          iss >> a >> b >> c >> d;
          if (a == "if")
          {
                if (b == TapeVector[RWH])
                {
                    i = labels[d];
                    continue;
                }
                else
                ;
          }
          else if (a == "goto") 
          {
               i=labels[b];
               continue;
          }
          else if (a == "write")
          {
               b = TapeVector[RWH];
          }
          else if (a == "right")
          {
               ++RWH;
          }
          else if (a == "left")
          {
               --RWH;
          }
          else if (a == "halt")
          {
               goto end;
          }
          else
          {
              continue;
          }
          ++i;
    } //end while
    end:
    for (int k=0;k<TapeVector.size();++k) 
          {  
               cout << TapeVector[k];             
          }
    cout << endl;
    InFile.close();
    system("Pause");
    return 0;


}

这是我到目前为止所做的..但是我似乎不了解很多错误。

51 C:\Users\JHONIN\Documents\THISIS\readtest 2.cpp no match for 'operator=' in 'labels = lookupLabels(const std::vector<std::string, std::allocator<std::string> >&)()' 
55 C:\Users\JHONIN\Documents\THISIS\readtest 2.cpp variable `std::istringstream iss' has initializer but incomplete type 

您不能只在输入文件中跳到未知位置。 您需要阅读整个程序,将其存储在数据结构中的内存中,至少使用标签进行注释,然后在该结构中跳转,例如:

typedef map<string, size_t> Labels; // map label -> position in Instructions vector;

Labels lookupLabels(const vector<string>& instr) {
    Labels ret;
    for (size_t i = 0; i < instr.size(); ++i) {
        const string& s = instr[i];
        size_t colonpos = s.find(':');
        if (colonpos != string::npos)
            ret[s.substr(0, colonpos)] = i;
    }
    return ret;
}

现在,您可以用相同的方式修改“问题分析”,即在指令向量中而不是文件中循环行。 按下goto只需在labels选择另一个行号并继续进行即可,大致就像这样

labels = lookupLabels(Instruction)
size_t i = 0;
while (i < Instruction.size()) {
    istringstream iss(Instruction[i]);
    // ...
    iss >> a;
    if (a == "goto") {
        string label;
        iss >> label;
        i = labels[label];
        continue;
    }
++i;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM