How to read multiple text files in c++?

This is my code for one file:

#include <fstream>
#include <string>
using namespace std;


int main()
{

fstream file;
string word0,word1,word2, filename1;

cout << "Please enter the 1st file: " << endl;
cin >> filename1;

// opening file
file.open(filename1.c_str());

// extracting lines from the file
while (file >> word)
{
// displaying content
getline(file, word);
cout << word << endl;
}

return 0;
}
http://www.192168101.com
https://19216811.dev/
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::string file_name ;

    // repeat for each file name entered by the user
    while( std::cout << "\nenter file name (enter an empty string to quit): " &&
           std::getline( std::cin, file_name ) && !file_name.empty() )
    {
        if( std::ifstream input_file{file_name} ) // if the file was opened for input
            std::cout << '\n' << input_file.rdbuf() ; // display its contents

        else std::cerr << "failed to open input file '" << file_name << "'\n" ;
    }
}
Topic archived. No new replies allowed.