Hello! You need to write a program that copies the code entered from the keyboard (up to the emulated end of the file) to a file whose name will be transferred on the command line.
But when you call the command line (first I write the address of the program, then the file name), the program immediately writes Output is complete, but no output occurs - the file is neither created nor replaced. Please explain what the problem is.
#include <iostream> #include <fstream> #include <string> int main(int argc, char* argv[]) { using namespace std; if (argc != 2) { cout << "INVALID CMD ARGUMENTS.\n"; exit(EXIT_FAILURE); } else { char ch = '0'; ofstream fout(argv[1]); cout << "Enter a text: " << endl; while (fout.is_open() && !cin.eof()) { cin.get(ch); fout << ch; } } cout << "Output is complete!\n"; system("pause"); return 0; } Update
I enter the text, press ^Z, Enter, опять ^Z (otherwise it doesn’t work) and the file is simply not created. he writes that Output is complete and that's it.
And another important condition - without using cmd, everything works as it should.
Update 2
Here is my conclusion
C:\Users\Никита>D:\Dropbox\Pascal\C++\project1\Debug\project1.exe a.txt argv[0]: D:\Dropbox\Pascal\C++\project1\Debug\project1.exe argv[1]: a.txt fout.is_open(): 1 Enter a text: ABCDEFGH 125 ^Z Output is complete! Для продолжения нажмите любую клавишу . . . Update 3
a.txt is not created at all
Important amendment! If you add it to the end (fin - ifstream object):
while(!fin.eof()) { fin >> ch; cout << ch } it displays everything - as if everything was created.
fout. Check before cycle and display error message. - VladDcin.eof()returntrue? Or does it enter the cycle? - VladD