Can someone help me make a code for a username and password program where the usernameand pass is stored in a txt file.
I've tried many things but I still can't make it work.

The text file in question has the username password and user type separated by tabs and newlines

ex.

meuser mepassword Administrator
another pass2 Guest

I've been using fscanf but it doesn;t do anything after I input my user name and password.

Recommended Answers

All 12 Replies

could you post your code so we may see what you did

include stdio.h
include string.h

int main(void)
{
    char user[257];
    char pass[257];
    char userc[257];
    char passc[257];
    char usert[257];
    int loop=0;

    FILE *ca;
    ca=fopen("CA_users.txt", "w");

    printf("Please input your username: ");
    scanf("%s", user);
    printf("Please input your password: ");
    scanf("%s", pass);

    while (loop!=0)
    {
        fscanf(ca, "%s", userc);
        if (strcmp(userc, user)==0)
        {
            fscanf(ca, "%s", passc);
            if (strcmp(passc, pass)==0)
            {
                fscanf(ca, "%s", usert);
                printf("Welcome %s %s", usert, user);
                loop=0;
            }
        }
        else loop=1;
    }
}

This is what I've done so far and I still have to connect this to another program. I'm totally new at file manipulation so this is the best I can do.

The CA_users.txt has a format:
Username -tab- Password -tab- User_type

ca=fopen("CA_users.txt", "w");

when you use "w" for writing your erasing the previous contents of the file
that should be set to "r for reading only or "r+" for reading and writing without erasing

still doesn't work. changer it to r and done some tweaks still don't work.

-double post-

include stdio.h
include string.h

first And foremost it's

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

then...just noticed something

int loop=0;

if loop is initially 0 it would never pass the while loop in the first place

also is that the whole code? don't forget to close the file after opening

The whole is very long and is about converting currencies to another currency. But the first obstacle is the user log-in that determines the command that the user can do. The only problem I encountered is related with the file manipulation.

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

int main(void)
{
    char ut[14];
    char user[257];
    char pass[257];
    char userc[257];
    char passc[257];
    char usert[257];
    int loop=1;
    FILE *ca;
    ca=fopen("CA_users.txt", "r+");

    printf("Please input your username: ");
    scanf("%s", user);
    printf("Please input your password: ");
    scanf("%s", pass);

    while (loop!=0)
    {
        fscanf(ca, "%s", userc);
        if (strcmp(userc, user)==0)
        {
            printf("Welcome %s ", user);
            loop=0;
        }
    }
fclose(ca);
}

I did the code above to see if the strcmp of user and userc workd but it doesn't. I know that the file exists but I can't make it work.

I tried the program and I got it to work... are you sure that the text file is in the same directory as the c file?

What I meant that it didn't work is when there are 2 or more username and password in the text file. I'm currently thinking of a way how it will go to the next line to search for another username.

try to read while it's not the end of file... something like this

while (1)
{
    fscanf(ca, "%s %s %s", userc, pass ,v);
         if ( feof(ca) ) 
          break;
    printf("%s, %s, %s\n", userc, pass, v);
}

yes zeroliken is correct
Below is the modified version of your code which works fine

#include<stdio.h>
#include<conio.h>
#include<string.h>

int main()
{
    char user[256];
    char pass[256];
    char userc[256];
    char passc[256];
    FILE *ca;
    clrscr();
    ca=fopen("users.txt","r");
    printf("please enter the name: ");
    scanf("%s",&user);
    printf("please enter the password: ");
    scanf("%s",&pass);
    while(fscanf(ca,"%s%s",&userc,&passc)==2)
    {
        if((strcmp(userc,user)==0) && (strcmp(passc,pass)==0))
        {
            printf("welcome");
            break;
        }
    }
    fclose(ca);
    getch();
    return 0;
}

Got the program to wok thanks guys. Now to time to rewrite the whole program <_<

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.