1

I want to make simple login,register system. and register part is done, but login part is little confuse..

Login with Txt file. Filename - ID, file including "Nickname|PW"

then scanf ID first, if id exist -> get PW and open ID txt file to compare pw.

I'm tried fgets and fread and more... but i dont know exactly how to use them

int ckk; // position of "|"

ck_pw = fopen(fpp_id, "r"); //open ID txt file to read including text

for (int j = 0;; j++) { // count where is "|"
    char buf; // buffer
    buf = '\0'; //reset buf
    fread(buf, j, 1, ck_pw);
    if (buf == '|') { //find "|"
        ckk = j + 1;
        break;
    }
}

fseek(ck_pw, ckk, SEEK_CUR); //move cursor to "|"

for (int k = 0;; k++) { //read text before space(NULL)
    fread(pw_ck, k, 1, ck_pw);
    if (pw_ck[k] == NULL) {
        break;
    }
}    

I used an uninitialized "ck_pw" local variable. in Visual studio 2017

4
  • You should check fread return value. Commented May 24, 2019 at 23:47
  • So is the code itself correct? Commented May 24, 2019 at 23:50
  • 1
    Yes? No? Define "correct". for (int j = 0;; j++) and for (int k = 0;; k++) are endless loops, in case there is no | in the file, pw_ck[k] == NULL - you are comparing char with a void* (assuming pw_ck is some char[?] pointer and is valid), the code is incomplete (main is missing and relevant #includes, also the declarations of ck_pw, fpp_id, pw_ck etc. is missing), you don't check the return value of fread nor of fopen nor of fseek. The second loop doesn't make any sense, you are reading k characters and increment k endlessly, the code will not work as intended Commented May 25, 2019 at 0:04
  • There are many existing posts on this site about reading lines from text files using C. Have you read any of those to see if they solve your problem? Commented May 25, 2019 at 0:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.