2022-08-17

Printing sections of a text file with C

I'm looking for a solution on how to print only relevant sections from a log file where I want to display the code and its contents which spans multiple lines.

The log file is structured like this:

----- start -----
CODE: 1111

DESC: this is some descriptions
      which spans multiple lines.
-----  end  -----

----- start -----
CODE: 2222

DESC: this is some information
      for another code.
-----  end  -----

----- start -----
CODE: 1111

DESC: here is some more info
      for the code again.
-----  end  -----


What I like to achieve if to parse the log file using C, to generate a result similar to this.

----- start -----
CODE: 1111

DESC: this is some descriptions
      which spans multiple lines.
-----  end  -----

----- start -----
CODE: 1111

DESC: here is some more info
      for the code again.
-----  end  -----

I have tried various while loops reading the file into a buffer and compared the strings with strstr() etc. but haven't find any logic with if statements that works for me. I have tried to explore the use of fseek(), fgetpos(), fsetpos() etc. I have searched forums and blogs for answer that can help me forward with little success.

If anyone reading this has a solution to share or any pointer on how I should tackle this, or places to find such information would be much appreciated.



No comments:

Post a Comment