Terminating a c++ program with text files

Good day!

I've written a program that writes into a text file and reads from the same file. When I run it and put the required content of the file, I don't know how to terminate it so that it stops writing into the file and does the next step that reads and displays.

Can anyone please help me?
Here is my code:
#include <iostream>
#include <fstream>
using namespace std;

int main()
char ch;
ofstream fout("SnrLecturers.txt",ios::app);

while (cin.get(ch))
fout << ch;


ifstream fin("SnrLecturers.txt");
cout << ch;

return 0;

Thank you.
Press Ctrl-Z to terminate the input. cin will see that as an end of file and drop out of the while loop.
Thank you for your response.

I've tried that, I pressed Ctrl-Z and ^Z appears, then I pressed enter and the program terminated and does not go through the code where I read from the file. I am using Code Blocks.

Thank you!
Did you press Enter after to ^Z? On Windows F6 sent the same signal I think, but need to press Entr to "send" the line to stdin.

An alternatrve it to put your input into a file and redirect the file to stdin for your app. For example:
 ./myapp < input.txt
Last edited on

Apologies for the late response.

Thank you very much for your help, it is now working :-)
Topic archived. No new replies allowed.