Reading Lines by Lines From a File to a Vector in C++ STL Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Prerequisites: STL in C++Vector in C++File handling in C++ The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc. It is a library of container classes, algorithms, and iterators. Vector in C++ STL Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Reading Lines by Lines From a File to a Vector in C++ STL In this article, we will see how to read lines into a vector and display each line. We will use File Handling concepts for this. Reading Files line by line First, open the file i.e. //open the file ifstream file("file.txt"); Now keep reading the next line and push it in vector function until the end of the file i.e. string str; // Read the next line from File until it reaches the end. while(file >> str){ //inserting lines to the vector. v.push_back(str);} Example 1: C++ // C++ Program to implement Reading // In Lines From A File To A Vector #include <bits/stdc++.h> using namespace std; int main() { // Opening the file ifstream file("file.txt"); vector<string> v; string str; // Read the next line from File until it reaches the // end. while (file >> str) { // Now keep reading next line // and push it in vector function until end of file v.push_back(str); } // Printing each new line separately copy(v.begin(), v.end(), ostream_iterator<string>(cout, "\n")); return 0; } Output: Output for the code with the file name "file.txt" Example 2: C++ // C++ Program to read lines // From A File To A Vector #include <fstream> #include <iostream> #include <string> #include <vector> using namespace std; int main() { fstream myfile; // open file myfile.open("file1.txt"); vector<string> g1; if (myfile.is_open()) { // checking if the file is open string str; // read data from file object // and put it into string. while (getline(myfile, str)) { g1.push_back(str); } // close the file object. myfile.close(); } cout << "\nVector elements are: " << endl; for (int i = 0; i < g1.size(); i++) { cout << g1[i] << endl; } } Output Output of the vector elements Comment More infoAdvertise with us Next Article Reading Lines by Lines From a File to a Vector in C++ STL S soumyadeeppaul022 Follow Improve Article Tags : Technical Scripter C++ Technical Scripter 2022 STL cpp-vector +1 More Practice Tags : CPPSTL Similar Reads Bash Scripting - How to read a file line by line In this article, we are going to see how to read a file line by line in Bash scripting. There might be instances where you want to read the contents of a file line by line using a BASH script. In this section, we will look at different ways to do just that. We will use BASH commands and tools to ach 3 min read How To Read a File Line By Line Using Node.js? To read a file line by line in Node.js, there are several approaches that efficiently handle large files and minimize memory usage. In this article, we'll explore two popular approaches: using the Readline module (which is built into Node.js) and using the Line-reader module, a third-party package. 3 min read Copy File To Vector in C++ STL Prerequisite: Vectors in C++ STLFile Handling in C++ The C++ Standard Template Library (STL) provides several useful container classes that can be used to store and manipulate data. One of the most commonly used container classes is the vector. In this article, we will discuss how to copy the conte 2 min read How to Get Vector Pointer to Raw Data in C++? In C++, vector is a dynamic array that stores the data in the contiguous memory allocated during runtime. It internally keeps the pointer to this raw data memory. In this article, we will learn how to get pointer to the vector's raw data.The easiest method to get the pointer to raw data is by using 2 min read Initializing Vector using an Existing Vector in C++ STL A vector is a type of container which can store objects of similar data type. Vector acts like a dynamic array where we can insert elements and the size of the array increases depending upon the elements inserted. Syntax: vector<data_structure/type> vector_name(size, item) To know more about v 5 min read Different Ways to Convert Vector to Array in C++ STL In C++, vectors are dynamic arrays that can resize according to the number of elements. In this article, we will learn how to convert a vector to a C-style array in C++.The easiest way to convert a vector to array is by creating a new array and copy all the elements of vector into it using copy() fu 3 min read How to Sort a Vector in Descending Order Using STL in C++? Sorting vector in descending order means arranging the elements in such a way that the first element will be largest, and second element will be second largest and so on.In C++, the simplest way to sort the vector in descending order is to by using the sort() function with a custom comparator.C++#in 3 min read Read a record from a File in C++ using seekg() and tellg() Given that a binary file "student.data" is already loaded in the memory of the computer with the record of 100 students, the task is to read the Kth record and perform some operations.seekg() is a function in the iostream library (part of the standard library) that allows you to seek to an arbitrary 2 min read Implement your own tail (Read last n lines of a huge file) Given a huge file having dynamic data, write a program to read last n lines from the file at any point without reading the entire file. The problem is similar to tail command in linux which displays the last few lines of a file. It is mostly used for viewing log file updates as these updates are app 4 min read How to use getline() in C++ when there are blank lines in input? In C++, if we need to read a few sentences from a stream, the generally preferred way is to use the getline() function as it can read string streams till it encounters a newline or sees a delimiter provided by the user. Also, it uses <string.h> header file to be fully functional. Here is a sam 2 min read Like