STD has a function called Getline, which is to read each line from the input file and save the line in a string, each line is identified by a newline character (\ n).
It's prototype is,
istream& Getline (istream& is, string& str);
If you want to traverse an input text file, you can write the following code:
#include <fstream> #include <string> #include <iostream>using namespace Std;ifstream input_file (" Input.txt "); string Single_line;while (Getline (Input_file, Single_line)) {cout << single_line << Endl;}
When the end of the file is encountered, the function automatically stops extracting the string, then the corresponding while loop condition is judged false and jumps out of the loop.
The next step is to pick out those lines that match a certain pattern, such as picking out all the rows that contain a substring. At this point, you use the Find function of the string class.
if (Single_line.find ("Node id")! = String::npos) {//perform Operations}
This article from "Hu A Knife" blog, declined reprint!
C + + FAQ Read File Getline