除了系统(“暂停”)之外,是否还有一个选项用于保持可执行文件处于打开状态? 我试图从C++中运行凯撒程序,一旦调试,它就不会保持打开状态。怎么办
我使用系统(“暂停”)无效。我还尝试了getchar(),它在其他应用程序中也可以使用,但不适用于此可执行文件除了系统(“暂停”)之外,是否还有一个选项用于保持可执行文件处于打开状态? 我试图从C++中运行凯撒程序,一旦调试,它就不会保持打开状态。怎么办,c++,system,caesar-cipher,pause,C++,System,Caesar Cipher,Pause,我使用系统(“暂停”)无效。我还尝试了getchar(),它在其他应用程序中也可以使用,但不适用于此可执行文件 #include <iostream> #include <fstream> #include <string> #include <sstream> using namespace std; /** Encrypts a stream using the Caesar cipher @param in- the stream to
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
/**
Encrypts a stream using the Caesar cipher
@param in- the stream to read from
@param out- the stream to write to
@param k- the encryption key
*/
void encrypt_file(ifstream& in, ofstream& out, int k)
{
char ch;
while (in.get(ch))
{
out.put(ch + k);
}
}
int main(int argc, char* argv[])
{
int key = 3;
int file_count = 0; // The number of files specified
ifstream in_file;
ofstream out_file;
for (int i = 1; i < argc; i++) //Process all command-line arguments
{
string arg = argv[i]; // The currently processed argument
if (arg == "-d") // The decryption option
{
key = -3;
}
else // It is a file name
{
file_count++;
if (file_count == 1) // The first file name
{
in_file.open(arg.c_str());
if (in_file.fail()) // Exit the program if opening failed
{
cout << "Error opening input file " << arg << endl;
return 1;
}
}
}
}
if (file_count != 2) // Exit if the user didn't specify two files
{
cout << "Usage: " << argv[0] << " [-d] infile outfile" << endl;
return 1;
}
encrypt_file(in_file, out_file, key);
getchar();
//system("pause");
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
/**
使用凯撒密码加密流
@param in-要从中读取的流
@param out—要写入的流
@param k-加密密钥
*/
void encrypt_文件(ifstream&in、ofstream&out、int k)
{
char ch;
while(in.get(ch))
{
输出(ch+k);
}
}
int main(int argc,char*argv[])
{
int键=3;
int file_count=0;//指定的文件数
_文件中的ifstream;
流式输出文件;
for(int i=1;i cout您实际上并没有将ofstream指向任何地方。据我所知,ofstream将缓冲输入,直到您将其指向某个地方为止(免责声明,我可能错了)
无论如何,由于您没有为out_文件打开任何内容,它将不会写入您希望它写入的任何文件。因此,看起来您的程序没有执行您想要的操作,只是返回。您可以尝试使用系统(“暂停/nul”);我在VS中制作程序时使用它,因为它们不会保持打开状态,但我只将它们放在返回0之前;因此,我按下按钮后它们会关闭,但我认为它可以在任何地方工作,请尝试一下。程序将运行到main完成。返回的事实意味着执行完成(显示任何错误)。它看起来也不像是在任何地方定义out\u文件?out\u文件
声明在main()
的顶部,在in\u文件
的下面,但是out\u文件
在encrypt\u文件()
被调用。@RemyLebeau它是声明的,而不是定义的…它应该写入哪里?这可能是一个XY问题。听起来可能你真正想要的是控制台在应用程序终止后保持打开状态。这应该是在你的操作系统/窗口系统/开发环境中配置的,而不是你想要的uld是您程序的一部分。可能重复“据我所知,ofstream将缓冲输入,直到您将其指向某个地方(免责声明,我可能是错的)”-如果未打开ofstream的,它将不会在任何地方写入,并且将设置其坏位状态。