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.

  • Obviously, the program cannot open fout . Check before cycle and display error message. - VladD
  • @ dbs1024: hm. Strange. And why is the cycle not included? Did cin.eof() return true ? Or does it enter the cycle? - VladD
  • @ dbs1024: Well, and you write "immediately writes Output is complete". So, not immediately? Describe exactly what is happening. - VladD
  • @ dbs1024: try to trace which characters (codes) come to you in a loop. - VladD

2 answers 2

@ dbs1024 , probably somehow not so you check the result.

Basically, this code should work.

(Only an extra \n (more precisely, the last character entered before ^ Z) will be written to the output file.

Understand why? )

And why constantly check fout.is_open() I do not understand. Are you afraid that something broke while recording?

Then bring it to the end. Analyze after while and write diagnostics.

Update

@ dbs1024 , I have (in Win XP MinGW)

 C:\Documents and Settings\avp\src\hashcode>g++ c.cpp C:\Documents and Settings\avp\src\hashcode>a INVALID CMD ARGUMENTS. C:\Documents and Settings\avp\src\hashcode>a xaxa Enter a text: 12345 zxcv ^Z^Z Output is complete! Для продолжения нажмите любую клавишу . . . C:\Documents and Settings\avp\src\hashcode>type xaxa 12345 zxcv C:\Documents and Settings\avp\src\hashcode> 

Your code somehow works.

Update 2

@ dbs1024 , to format the code (run results) in the comments, simply separate the formatted text from the rest with blank lines and enter 4 spaces before each line .

For example (further empty line)

 а это текст с 4 пробелами перед каждой строкой дальше опять пустая строка... 

and again the text of the comment.

-

Do you have a.txt created by the program empty?

-

In general, it is better to learn C / C ++ in Linux (possible under the virtual machine). Then, if you want (or really need), master Windows.

Update 3

@ dbs1024 , try adding

 #include <unistd.h> int main ... { using namespace std; char wd[1024]; getcwd(wd, 1024); cout << "WD = [" << wd << "]\n"; 

at the beginning of the program. In the printed table of contents and look for your a.txt .

    @ dbs1024 : so how is it not created? And if you specify the full path to the file? I think it is just created in an unexpected place.

    • YES! Created! Thank you very much for your help - dbs1024