I'm trying to retrieve certain lines from a text file. I'm using:
#include <iostream> #include <fstream> using namespace std; void parse_file() { std::ifstream file("VampireV5.txt"); string str= "Clan"; string file_contents; while (std::getline(file, str)) { file_contents += str; file_contents.push_back('\n'); } cout << file_contents; file.close(); } int main() { parse_file(); return 0; } I want to get that one and only line containing "Clan" until '\n'. I've tried to add an if inside the while loop but it is not returning anything.
Is there a way to make it get 1 line at once?
if (str.find("Clan") == 0)to check, whether the line starts with the word you're looking for.str. You may find related information here. Then, you can break in the while loop when you got this line