Reading a text file (*.txt) to std::string array, c++17 [duplicate]

1 day ago 1
ARTICLE AD BOX

Keeping it simple, here's a program that reads into a std::vector<std::string>, each slot contains a "line" of text:

#include <fstream> #include <string> #include <vector> int main() { std::ifstream text_file("dir/filename.txt"); std::vector<std::string> text_container; std::string text_line; while (std::getline(text_file, text_line) { text_container.push_back(text_line); } return EXIT_SUCCESS; }

You may want to reserve some room in the vector to prevent a lot of resizing.

Thomas Matthews's user avatar

1 Comment

The parentheses are unbalanced in the line with the while.

2026-05-19T01:12:05.02Z+00:00

Read Entire Article