gpt4 book ai didi

c++ - 系统()调用流文件的批处理可执行文件使程序在Windows上重置

转载 作者:太空宇宙 更新时间:2023-11-04 13:14:30 29 4
gpt4 key购买 nike

当我“构建并运行”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");

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

最佳答案

"You must explicitly flush—by using fflush or _flushall—or close any stream before you call system." - MS Documentation

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

关于c++ - 系统()调用流文件的批处理可执行文件使程序在Windows上重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38017956/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com