如何在文本文件中保存按键 我尝试用C++制作密钥记录器,我想保存文本文件中的任何键。 我可以知道按下了什么键,但我有问题,当程序想保存的关键,它只是保存最后一个键按下。我怎样才能解决这个问题 代码如下: #include<iostream> #include<string> #include<Windows.h> #include<conio.h> #include<fstream> using namespace std; int main() { system("color 09"); int asciiValue; char key; cout << "enter any key " << endl << endl; cout << "press ESC to exit.." << endl << endl; while (1) { key = _getch(); asciiValue = key; if (asciiValue == 27) { system("cls"); system("color 8a"); cout << "\n\n\n\n\n\n\n\n\t\t\t\tCLOSE" << endl << endl; Sleep(1000); exit(1); } cout << "key pressed is : \" " << key << " \"" << "his Value = " << asciiValue << endl << endl; ofstream o("keylogger.txt"); o << key; } cin.ignore(1); return 0; } #包括 #包括 #包括 #包括 #包括 使用名称空间std; int main() { 系统(“颜色09”); 内华达州; 字符键; cout
您在每次迭代中都会创建一个新文件。如何在文本文件中保存按键 我尝试用C++制作密钥记录器,我想保存文本文件中的任何键。 我可以知道按下了什么键,但我有问题,当程序想保存的关键,它只是保存最后一个键按下。我怎样才能解决这个问题 代码如下: #include<iostream> #include<string> #include<Windows.h> #include<conio.h> #include<fstream> using namespace std; int main() { system("color 09"); int asciiValue; char key; cout << "enter any key " << endl << endl; cout << "press ESC to exit.." << endl << endl; while (1) { key = _getch(); asciiValue = key; if (asciiValue == 27) { system("cls"); system("color 8a"); cout << "\n\n\n\n\n\n\n\n\t\t\t\tCLOSE" << endl << endl; Sleep(1000); exit(1); } cout << "key pressed is : \" " << key << " \"" << "his Value = " << asciiValue << endl << endl; ofstream o("keylogger.txt"); o << key; } cin.ignore(1); return 0; } #包括 #包括 #包括 #包括 #包括 使用名称空间std; int main() { 系统(“颜色09”); 内华达州; 字符键; cout,c++,C++,您在每次迭代中都会创建一个新文件。 只要说一句话: ofstream o("keylogger.txt"); 在while循环之前以每次循环迭代的方式打开流o的将重新打开文件,并在写入之前清除其内容。在循环之前打开文件,或者阅读流的文档,了解如何在追加模式下打开文件(这不会清除内容,但会附加到文件中)。如果我在循环时将其输出,则不会这样做,因为它不能在循环时输出loop@IbrahimTEslim我不明白为什么不能:of stream o(“keylogger.txt”);while(cond
只要说一句话:
ofstream o("keylogger.txt");
在while循环之前以每次循环迭代的方式打开流o的
将重新打开文件,并在写入之前清除其内容。在循环之前打开文件,或者阅读流
的文档,了解如何在追加模式下打开文件(这不会清除内容,但会附加到文件中)。如果我在循环时将其输出,则不会这样做,因为它不能在循环时输出loop@IbrahimTEslim我不明白为什么不能:of stream o(“keylogger.txt”);while(cond){…}返回0;
while(cond)是什么平均值???@IbrahimTEslim在您使用的情况下,可以进行测试,但在实际情况下,您可能需要一个条件bool cond
来打破while循环并完成程序。只是一个细节,可能是主题外的。。。