There is a simple example. I open the cmd command line, enter the path to * .exe and the name of the text document: C: \ Windows \ system32> "... exe" "in1.txt". At the exit I get
first
Hello!
And second'a no!
The problem is in reading through the command line, if this manipulation is removed, then the second is perfectly displayed.

#include <string> #include <fstream> #include <iostream> using namespace std; void first(string& s1,ifstream& in) { in >> s1; // тут считывается файл со строкой first in.close(); } void second(string& s2) { ifstream in2("in2.txt"); //тут записано second in2 >> s2; in2.close(); } int main(int argc, char **argv) { if (argc != 2) { cerr << "\nError.";exit(-1); } ifstream in(argv[1]); string s1,s2; first(s1, in); //in.close(); second(s2); cout << s1<<endl; cout << "Hello!"<<endl; cout<< s2; system("pause>void"); return 0; } 

How to solve this problem?

  • 2
    Why do you write the result of the pause command in the void file? - Harry
  • Just learn good practice: always check errors when working with files and display detailed diagnostics if something is wrong. And learn how to use the debugger. 99% of questions like this immediately disappear. - PinkTux
  • What is there to guess: second is not, because you do not have an in2.txt file. - AnT

1 answer 1

It works as intended, if in the command line you first go to the folder with the program and its files.

Or specify the full paths to the files - both in the text of the program, and in the console.