0

I made a keylogger and it works great. The only problem that I have is it outputs the keylogger to the txt file just after I close the console. I'd to write in the txt file while I use the keylogger and not that I'd output the keylogger just after I close the console. I am now using fstream to write the file. This is what I am doing right now to print the keylogger the the txt file

ofstream writeFile;
writeFile.open("test.txt"); // opening file
writeFile << key;

1 Answers1

3

You need to flush a buffered stream in order to guarantee saving your output before a close:

writeFile << key << std::flush;

Your other option is to unbuffer the stream:

ofstream writeFile;
writeFile.rdbuf()->pubsetbuf(0, 0); // unbuffer stream
writeFile.open("test.txt"); // open file
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • Thanks! Do you know also why is "\b" dosen't work in the file? It does "" insted of deleting the text –  Jul 31 '19 at 22:53
  • 1
    Because `'\b'` is part of the `char` stream, your terminal handler performs the magic to move the cursor backwards and overwrite the previous `char`. – Paul Evans Jul 31 '19 at 23:02
  • So there is no way deleting char? –  Jul 31 '19 at 23:26
  • Deleting isn't normally possible. You can overwrite though. Just step the `ofstream` pointer back and write a new value. Note: `\b` is not an instruction, it's just another value to add to the stream - and you can truncate files - but not streams, if that's needed. – Ted Lyngmo Aug 01 '19 at 00:39
  • @TedLyngmo How can I pointer it back and write a new value? –  Aug 01 '19 at 00:48
  • @Eathat If it's a file stream, just use [seekp](https://en.cppreference.com/w/cpp/io/basic_ostream/seekp) when writing. or `seekg` for reading (on a `istream` type). That being said, if you are doing a keylogger, you want even backspaces and timings logged. :-) – Ted Lyngmo Aug 01 '19 at 00:49
  • 1
    @TedLyngmo got it. Thanks. What do you mean "if you are doing a keylogger, you want even backspaces and timings logged." –  Aug 01 '19 at 00:54
  • A keylogger worth the name lets you investigate what the user actually did - and pauses and backspaces tells you a lot. It also helps identifying who actually wrote the stuff afterwards. We all have unique patterns. So, each keystroke should be logged with timing if this is going to be used in a forensic analysis etc. – Ted Lyngmo Aug 01 '19 at 01:01
  • @TedLyngmo I see, for somereason my seekp does not work this is what I did I posted it above as updated –  Aug 01 '19 at 01:13
  • Sounds like a new question. It's hard to read in this comment format. – Ted Lyngmo Aug 01 '19 at 01:14
  • @TedLyngmo I posted it in my qustion for some reason I need the approve of the guy who posted the answer. –  Aug 01 '19 at 01:15
  • 1
    Seams reasonable, since you are changing a question with an accepted answer. I think you should create a new question. – Ted Lyngmo Aug 01 '19 at 01:16
  • @TedLyngmo My falut There. I changed my qustion. –  Aug 01 '19 at 01:17
  • 1
    Make a new question instead. The original question has an answer. Don't add to it. – Ted Lyngmo Aug 01 '19 at 01:18
  • @TedLyngmo You are right. I will. What do you think the title should be? I can't think of a good one to be honest..... –  Aug 01 '19 at 01:19
  • Revert the latest edit of this question so that others with similar questions may benefit from reading it later. Then, take the parts you now need clarifying to the new question into a new mininal example. – Ted Lyngmo Aug 01 '19 at 01:21
  • @TedLyngmo I did, I know but I still can't think of a good title. The best title I have is "why seekp Isn't working in my code". Which is a bad title. –  Aug 01 '19 at 01:22
  • @Eathat I'm afraid that's a part of programming too :-) Describing your problem. Deal with what you have now for awhile and then, if you don't make it work, pose another question. It'll be easy to formulate then I think. – Ted Lyngmo Aug 01 '19 at 01:23
  • @TedLyngmo you are right. I'll ask it tommorow anyway. It's late or should I say morning. Good night/morning I guess. –  Aug 01 '19 at 01:27
  • @Eathat Cheers! – Ted Lyngmo Aug 01 '19 at 01:27