[Help] how to read two files from terminal

Hi

the code below helps but I can't seem to figure out how to make my program read from to files.

Example:
# g++ hello.cpp -o hello
# hello read.txt this.txt

would really help if someone would explain it line by line, too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <fstream> // for file-access
#include <string>
#include <iostream>
using namespace std; 
int main(int argc, char* argv[])
{ 
    if (argc > 1) {
        cout << "argv[1] = " << argv[1] << endl; 
    } else {
        cout << "No file name entered. Exiting...";
        return -1;
    }
    ifstream infile(argv[1]); //open the file
    
    if (infile.is_open() && infile.good()) {
        cout << "File is now open!\nContains:\n";
        string line = "";
        while (getline(infile, line)){
            cout << line << '\n';
        }
        
    } else {
        cout << "Failed to open file..";
    }
    return 0;
}




Taken From: https://www.daniweb.com/programming/software-development/threads/238224/help-to-read-a-txt-file-using-command-line-arguments-in-visual-c
Roughly along the lines of
1
2
3
4
for ( int i = 1 ; i < argc ; i++ ) {
    ifstream infile(argv[i]);
    // do stuff
}
Thank you! it works now!
Topic archived. No new replies allowed.