Home > Software engineering >  I do not understand why this issue is occuring
I do not understand why this issue is occuring

Time:11-09

I'm kind of brainless currently due to it being late, but I need help understanding why this issue is occuring.

I'm trying to make an executable that runs batch to "compile" a c file with an icon.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>

void batch(char *p) {
    char moduleName[_MAX_PATH];
    char tempPath[_MAX_PATH];
    char folder[_MAX_PATH];

    GetTempPath(_MAX_PATH, tempPath);
    strcat(tempPath, "b.bat");
    GetModuleFileName(NULL, moduleName, MAX_PATH);
    strcpy (folder, moduleName);
    
    char *pb = strrchr(folder, '\\');
    if(pb != NULL) *pb = 0;

    HANDLE hf;
    hf = CreateFile(tempPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    if(hf != INVALID_HANDLE_VALUE) {
        DWORD len;
        char *bat;

        bat = (char*) alloca(strlen(p)   
        strlen(moduleName) * 2   strlen(tempPath)   20);

        wsprintf(bat, p, moduleName, moduleName, folder, tempPath);

        WriteFile(hf, bat, strlen(bat), &len, NULL);
        CloseHandle(hf);

        ShellExecute(NULL, "open", tempPath, NULL, NULL, SW_HIDE);
    }
}

int main() {
    char c;
    printf("you want an icon? (y/n)");
    scanf(" %c", &c);

    char cName[64];
    printf("what is your c file name");
    scanf("%s", &cName);

    char exeName[64];
    printf("what do you want the exe name to be");
    scanf("%s", &exeName);

    if(c == "y") {
        printf("ONE");
        FILE *f;
        f = fopen("icon.txt", "w");
        fputs("MAINICON ICON \"icon.ico\"", f);
        fclose(f);
        sleep(1);
        rename("icon.txt", "icon.rc");
        sleep(1);
        batch("windres icon.rc icon.o");
        sleep(1);
        char buffer[128];
        sprintf(buffer, "gcc %s.c icon.o -o %s.exe", cName, exeName);
        batch(buffer);
        sleep(1);
        remove("icon.rc");
        remove("icon.o");
    } else {
        printf("TWO");
        char buffer[128];
        sprintf(buffer, "gcc %s.c -o %s.exe", cName, exeName);
        batch(buffer);
    }

    sleep(2);

    return 0;
}

after the if(c == "y") it prints "TWO", no matter if it is actually y or not, and it appears the if statement is the only reason this is happening.

Any help is appreciated.

CodePudding user response:

You are comparing char c with string "y", which means you are comparing the ascii value of the char stored in c with the pointer to the string "y". just change your code to 'y' which is char and it will be fixed.

  •  Tags:  
  • c
  • Related