In the program i made tried to login and it does not work i am not sure what i did wrong.
[Click Here](http://www.onlinegdb.com/B15uh6kB_)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXUSERNAME 30
#define MAXPASSWORD  20

typedef struct
{
    char username[MAXUSERNAME];
    char password[MAXPASSWORD];
} pass;

int main()
{
    char userName[MAXUSERNAME];
    char password[MAXPASSWORD];
    pass t;
    int c;
    FILE *P= fopen("teachers.txt", "rb");

    if (P == NULL) 
    {
        printf("Error! opening file");
        exit(1);
    }

    do
    {
        printf("\nUsername:");
        fgets(userName,MAXUSERNAME,stdin);
        printf("\nPassword:");
        fgets(password,MAXPASSWORD,stdin);
        if((!strcmp(userName, t.username)) && (!strcmp(password, t.password)))
        {
           printf("Welcome to the Ahjin Private Primary School Repot Renerating System");
        }
        else
        {
            printf("\nLogin Failed \nEnter Username & Password Again\n");
            c++;
        }
    }
    while(c<=3);
    if(c>3)
    {
        printf("Login Failed");
        printf("Sorry,Unknown User.");
    }
}

Dani AI

Generated

Primary problems in the posted code are straightforward and common: the program never reads any record from the opened file (so the struct t is uninitialized and comparisons always fail), the attempt counter c is never initialized (undefined behavior), and fgets leaves a trailing newline which makes strcmp fail even for correct input. correctly pointed out the missing file-read logic and raised the important security note about plain-text passwords; asked about errors but the runtime issues above are the root causes.

Practical fixes and tips:

  • Initialize the attempt counter before use (for example int attempts = 0;) and use a clear loop condition (e.g., while (attempts < 3)).
  • Strip the trailing newline from fgets results before comparing. A compact, portable way is to use strcspn to remove \r and \n.
  • Decide on the file format and read accordingly: if the file is text (recommended for debugging), read line-by-line and parse username/password; if it truly stores binary pass records, use fread to load each struct (be cautious about padding and portability). Always check fopen, fgets, and fread return values and fclose the file.

Example small fixes (string trimming + init):

int attempts = 0;
userName[strcspn(userName, "\r\n")] = '\0';
password[strcspn(password, "\r\n")] = '\0';

Security and testing advice: never store plain-text passwords in production — use a proven password-hashing algorithm (bcrypt/Argon2) and follow best practices such as those in the OWASP Password Storage Cheat Sheet (OWASP). Use compiler warnings and tools like Valgrind to catch uninitialized-variable bugs while debugging.

Recommended Answers

All 2 Replies

The code as is works fine. Since there is no code to read the file and look for a match, all logins fail.

I see you tagged this as homework. Remember that you get to design and write the missing code.

Also, given that passwords shall NEVER be stored in plain text in a file, this would get a failing grade from me.

More at https://blog.moertel.com/posts/2006-12-15-never-store-passwords-in-a-database.html

What is the error...?
Your code looks fine

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.