-1

So, i'm just practicing on my C programing skills and i wrote a program that gets a password and check it, if the password is correct so it call a MessageBox and show a message to user then it checks for existing file if the files is not existing it uses CreateFile and need to write some string inside.

from some reason it's not working and i can't understand why.

here is the code:

#include <stdio.h>
#include <Windows.h>
#include <string.h>
#pragma warning(disable:4996)


int main()
{
    char SecretPass[7] = "Az1236";
    char Userinput[7];
    int ResultCMP;


    printf_s("Insert Pass:");
    fgets(Userinput, 7, stdin);

    ResultCMP = strcmp(SecretPass, &Userinput);


    if (ResultCMP == 0)
    {
        int msg = MessageBox(NULL, "You Got In", "Wow", MB_ICONEXCLAMATION);
        if (msg == IDOK)
        {
            printf_s("Great! \n");
            char *szFileName = "C:\\Users\\shai\\Desktop\\Create\\kilroy.txt";

            FILE *fp = fopen(szFileName, "r");
            if (fp == NULL) {
                HANDLE h = CreateFileA(szFileName, GENERIC_READ | GENERIC_WRITE, NULL, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);

                FILE *fp;
                fp = fopen(szFileName, "w+");
                fprintf(fp, "testtest");
                puts("Done");
                fclose(fp);

            }

            else
            {
                printf("File exists!\n");
                getchar();
                return 0;
            }


        }

        else
        {
            printf_s("wrong pass!");
            getchar();
        }



    }
}
Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
Xozu
  • 81
  • 1
  • 2
  • 10
  • 1
    Can you give us the exact error message you are getting? I just tried it and it seems, as that the line ResultCMP = strcmp(SecretPass, &Userinput); causes at least a compile error. You could fix that by removing the & in front of Userinput. – Phillipp Mevenkamp May 18 '18 at 11:34
  • 2
    Why do you open the file with `fopen` "r" mode, then with `CreateFileA` in read/write mode and then again with `fopen` with "w+" mode? Your code makes no sense at all. What are you trying to achieve? – Jabberwocky May 18 '18 at 11:35
  • What's not working? Please explain exactly what is happening. – MFisherKDX May 18 '18 at 11:37
  • 1
    Hey Guys, never mind the problem was with createfile so i just used fopen and its ok now, thank you! – Xozu May 18 '18 at 11:43
  • *"from some reason it's not working and i can't understand why."* - Neither do we, because we do not know, what you expect, nor do we know, what you actually see. Read [ask] to improve this question. – IInspectable May 18 '18 at 16:38

1 Answers1

1

Hello if you use fopen (FILE *fopen(const char *filename, const char *mode)) then this should solve the issue, see code below:

if (ResultCMP == 0)
{
    int msg = MessageBox(NULL, "You Got In", "Wow", MB_ICONEXCLAMATION);
    if (msg == IDOK)
    {
        printf_s("Great! \n");
        char *szFileName = "C:\\Users\\shai\\Desktop\\Create\\kilroy.txt";

        FILE *fp = fopen(szFileName, "r");
        if (fp == NULL) {

            FILE *fp = fopen(szFileName, "ab+");


            fprintf(fp, "testtest");
            printf("Done");
            fclose(fp);

        }

        else
        {
            printf("File exists!\n");
            getchar();
            return 0;
        }

a+ or ab+ or a+b Append; open or create file for update, writing at end-of-file for fopen()

T.UK
  • 65
  • 2
  • 8