system()调用批处理可执行的流文件使程序重置在Windows上

时间:2016-06-24 16:21:28

标签: c++ batch-file

当我从Code :: Blocks中“构建并运行”程序时,它完美无缺!但是当我从“/ bin”文件夹手动运行它时,当它尝试使用system()调用“temp.bat”时,它会重置。这是为什么? 它没有要依赖的特定文件,它自己创建它们。 这是一些代码:

         ofstream filetmp1 ("temp1.bat", ios::out | ios::binary);
               filetmp1 << "@echo off" << '\n';
               filetmp1 << "echo user xyz> script.dat" << '\n';
               filetmp1 << "echo xyz>> script.dat" << '\n';
               filetmp1 << "echo get conntest.test>> script.dat" << '\n';
               filetmp1 << "echo bye>> script.dat" << '\n';
               filetmp1 << "ftp -n -s:script.dat xyz.com" << '\n';
               filetmp1 << "del script.dat" << '\n';
               filetmp1.close();
               system ("temp1.bat");            //IF I REMOVE THIS LINE, THE PROGRAM WORKS HOW IT SHOULD, until the second system("temp1.bat")
               system ("del temp1.bat");
               system ("cls");

    cout << "All data loaded!"; Sleep(1500);
    system("cls");

    ofstream filetmp ("temp1.bat", ios::out | ios::binary);
               filetmp << "@echo off" << '\n';
               filetmp << "echo user xyz> script.dat" << '\n';
               filetmp << "echo xyz>> script.dat" << '\n';
               filetmp << "echo cd app>> script.dat" << '\n';
               filetmp << "echo lcd data>> script.dat" << '\n';
               filetmp << "echo prompt n>> script.dat" << '\n';
               filetmp << "echo mput *.txt>> script.dat" << '\n';
               filetmp << "echo bye>> script.dat" << '\n';
               filetmp << "ftp -n -s:script.dat xyz.com" << '\n';
               filetmp << "del script.dat" << '\n';
               filetmp.close();
               system ("temp1.bat");                //WITH BOTH LINES REMOVED, IT WORKS. But I do not want to remove them, because I don't know alternative ways for them
               system ("del temp1.bat");
               system ("cls");

我想我已经说了所有的细节。提前谢谢。

1 个答案:

答案 0 :(得分:0)

  

“在调用系统之前,必须使用fflush或_flushall明确刷新 - 或关闭任何流。” - MS Documentation

尝试在system调用期间刷新所有需要保持打开的流。 _flushall()应该是最简单的。