2012-09-17 99 views
1

我目前正在研究一个项目,在这里我正在读取命令,并且需要避免空白行中的空格。我已经做到了这一点,但由于某种原因,我似乎无法弄清楚如何使其工作。我认为if(opcode == "\t" || opcode == " ")continue;会照顾,但它没有。如果有人可以请看看,帮我一把,那会很棒。从文件读取并避免空白行中的空白

这是我正在阅读的命令的一个小样本,它们在[标签] opcode [arg1] [,arg2]格式中。

#Sample Input 
LA 1,1 
LA 2,2 
\t <<<<<<<Here just to show that it's a blank line with only a tab 
TOP NOP 

这里是我的代码:

int counter = 0; 
int i = 0; 
int j = 0; 
int p = 0; 

while (getline(myFile, line, '\n')) 
{ 


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

    if (line[0] == '#') 
    { 
     continue; 
    } 


    // If the first letter isn't a tab or space then it's a label 

    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; 
     Symtablepos[i] = counter; 

     if(next>0) 
     { 
      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++; 
      } 

      if (next > 0) 
      { 
       delimeters = ", \n\t"; 
       current = next + 1; 
       next = line.find_first_of(delimeters, current); 
       arg1 = line.substr(current, next-current); 

       if (opcode == "WORDS") 
       { 
        counter += atoi(arg1.c_str()); 
       } 
      } 

      if (next > 0) 
      { 
       delimeters ="\n"; 
       current = next +1; 
       next = line.find_first_of(delimeters,current); 
       arg2 = line.substr(current, next-current); 

      } 
     } 

     i++; 

    } 

    // If the first character is a tab or space then there is no label and we just need to get a counter 
    if (line[0] == '\t' || line[0] == ' ') 
    { 
     string delimeters = "\t \n"; 
     int current; 
     int next = -1; 
     current = next + 1; 
     next = line.find_first_of(delimeters, current); 
     label = line.substr(current, next - current); 

    if(next>=0) 
     { 
      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++; 
      } 


      if (next > 0) 
      { 
       delimeters = ", \n\t"; 
       current = next + 1; 
       next = line.find_first_of(delimeters, current); 
       arg1 = line.substr(current, next-current); 

       if (opcode == "WORDS") 
       { 
        counter += atoi(arg1.c_str()); 
       } 

      } 



      if (next > 0) 
      { 
       delimeters ="\n\t "; 
       current = next +1; 
       next = line.find_first_of(delimeters,current); 
       arg2 = line.substr(current, next-current); 

      } 
     } 

    } 
} 

回答

1

使用std :: stringstream的,并从线的std :: string变量中读取。这样空白将被省略。

[更新] 如果你想从一开始删除空白空白:

s.erase(s.find_last_not_of(" \n\r\t")+1);

[UPDATE2]还是算了算话读书而:

就像这个例子:

#include <iostream> 
#include <sstream> 
#include <string> 

int main() { 

    std::string line; 
    while (std::getline(std::cin, line)) 
    { 
     std::string lineNoWS = line; 
     lineNoWS.erase(lineNoWS .find_last_not_of(" \n\r\t")+1); 
     if (lineNoWS.empty()) 
     std::cout << "EMPTY LINE\n"; 

     std::string word; 
     unsigned words = 0; 
     std::istringstream line_is(line); 
     while(line_is >> word) 
     { 
     std::cout << '\'' << word << "'\n"; 
     ++words; 
     } 
     std::cout << "(" << words << ")ENDLINE\n"; 
    } 
} 

只需将std :: cin替换为您的ifstream(file)即可。

+0

你介意解释如何做到这一点与空行。我以前用字符串流,但从来没有去除白色这样的空间 – cadavid4j

+0

看到我的更新,我添加了对跳过只有空格的行的支持 – PiotrNycz

+0

我想我只是不确定如何实现这个,而不改变我的整个程序 – cadavid4j

0

您应该尝试使命令的阅读更“通用”。 假设一行有效必须以标签开头,标签只能包含“字母”,而不是检查'\ t','\ n','\ r','#',(...) 为什么你不使用功能isalpha

然后,您需要获取参数,并假定它们由','分隔,最好的方法是根据','分隔符分割线。

一些示例代码,它为您提供“标签”和带有“参数”的矢量,我建议您也验证标签(例如,检查标签是否只包含字母,并假设您知道在“命令”验证了您检索特定标签参数的数量和类型。

std::ifstream inStream("c:\\data\\dump.txt"); 
    if(!inStream.fail()) 
    { 
     while(!inStream.eof())   
     { 
      std::string strLine; 
      std::getline(inStream, strLine); 

      // Label ? 
      if(isalpha(strLine[0])) 
      { 
       int iIndexOf = strLine.find(" "); 
       if(iIndexOf != string::npos) 
       { 
        std::string strLabel = strLine.substr(0, iIndexOf); 

        // Arguments ? 
        std::vector<std::string> vArguments; 
        std::stringstream ss(strLine.substr(iIndexOf, strLine.size() - iIndexOf)); 

        std::string strArgument; 
        while(std::getline(ss, strArgument, ',')) 
        {       
         if(strArgument.size()!=0)               
          vArguments.push_back(strArgument); 
        } 


        std::cout << "label: " << strLabel << std::endl << "arguments list: "; 
        for(size_t i=0; i<vArguments.size(); i++) 
         std::cout << vArguments[i] << ";"; 
        std::cout << std::endl; 
       } 
       else 
       { 
        // No Arguments 
        std::string strLabel = strLine;          
        std::cout << "label: " << strLabel << std::endl;      
       } 
      }   
     } 

     inStream.close(); 
    }