2017-06-24 53 views
-4

我试图通过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; 
} 
+1

开幕前'ofstream的O'你做重新打开每次循环的文件,并在写之前清除其内容。要么在循环之前打开文件,要么阅读'ofstream'的文档以了解如何以追加模式打开文件(不会清除内容,而是附加到文件)。 – Peter

回答

1

你正在创建在每次迭代的新文件。
只要把线:

ofstream o("keylogger.txt"); 

while循环

+0

它不会做,如果我把它放出来,而循环,因为它不能出去,而循环 –

+0

@IbrahimTEslim我不明白你为什么不能:'ofstream o(“keylogger.txt”); while(cond){...} return 0;' – Rama

+0

while(cond)是什么意思? –

相关问题