我试图通过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;
}
开幕前'ofstream的O'你做重新打开每次循环的文件,并在写之前清除其内容。要么在循环之前打开文件,要么阅读'ofstream'的文档以了解如何以追加模式打开文件(不会清除内容,而是附加到文件)。 – Peter