Home > front end >  How can ignore part of a line in a file with c ?
How can ignore part of a line in a file with c ?

Time:06-22

In the code below, you can see a self compiler thing I tried to/am still making. I'm wondering at the 2nd while loop (while(numOfLines <=line) {...}), how could I run the code there? The comments are either what the code does, or a question with the code that needs to be answered.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class Life {
  public:
    void compile() {
      //Get the main files content and store into a var...
      string txt;
      int line;
      //The file I want to get
      ifstream file("index.txt");
      string fileCont;
      int numOfLines = 0;
      //Get the lines of the file
      while(getline (file, txt)) {
        line  ;
      }
      //After that make a statment to get the #of lines to create a compile statment
      while(numOfLines <= line) {
        
        getline(file, txt);
        if(txt.rfind("say")) {
          //How can I ignore the say part and get the content inside the parentheses?
          //Ex: say("Hello World");
          //How could I store the content inside of the parentheses to a variable?
          cout <<"say command found" <<endl;
          txt.ignore("say");
          to_string(txt);
        }
        numOfLines  ;
        
      }
      
    }
};

CodePudding user response:

If you present a simplified version of algorithm and you really need the lines count, please consider reopening 'file' or move the file pointer to the beginning of the file : file::seekg( 0 )

Hope this helps.

CodePudding user response:

Here is a simplified code fragment for parsing a file, per my understanding of your requirements:

std::string text;
while (std::getline(file, text))
{
    std::string::size_type  found_position = 0U;
    found_position = text.find("say(");
    if (found_position != std::npos)
    {
        Validate_Text(text);
    }
}

The Validate_Text function will validate the given text and perform operations on the text.

  • Related