Home > database >  C# Comparing user input with data from external file C#
C# Comparing user input with data from external file C#

Time:02-28

Sorry if the post is duplicated, but I couldn't find any case like mine that is posted here or somewhere else.

I am working on a C# console application that should save the user input and then read it again "Something like a simple sign up and login application".

using System;
using System.IO;

namespace Reader
{
    class Program
    {
        static void Main(string[] args)
        {
            string filepath = @"C:\myProgramingFiles\password.txt";

            StreamReader reader = File.OpenText(filepath);

            string line = reader.ReadLine();
            Console.WriteLine("Write your username:");
            string userInput = Console.ReadLine();
            Console.WriteLine("Write your password:");
            string password = Console.ReadLine();
            Console.WriteLine(userInput);
            Console.WriteLine(password);

            while(line != null)
            {
                Console.WriteLine(userInput == line);
                Console.WriteLine(password == line);
                if (userInput == line)
                {
                    Console.WriteLine("Your username is: "   line);
                }
                if(password == line)
                {
                    Console.WriteLine("Your password is: "   line);
                }
                line = reader.ReadLine();
            }
            reader.Close();
        }
    }
}

I have this code that reads the data from password.txt, and everything works fine, but when I do the if-else it's first checks if both user inputs are the same as the username, and then it loops again and checks if both user inputs are like the password. Sorry if I couldn't make it clear, you can run this code if you want and mock up the password.txt, and check it.

It is actually a logical and expected result, but the thing is that I don't know how else I should do it. Can you please help me?

I have tried a lot of things that didn't work, and this was my last try, so I know that it is not the best code, but it explains the problem

CodePudding user response:

Let's suppose your password.txt file is like this (which appears to be the case):

password.txt

User1 
Pass123
User2
Pass456

etc.

The way your code is written, with that loop you have, if the user enters User1 for the username and Pass123 for the password, the output will be:

Your username is: User1
Your password is: Pass123

BUT if the same user enters User1 and Pass456 the output will be:

Your username is: User1
Your password is: Pass456

Which is obviously undesirable logic.

So what you need is to use your loop to check for the matching username, and only when that condition is met, check for the matching password.

Otherwise you even get results like this, if the user enters Pass123 for the username and Pass456 for the password:

Your password is: Pass123
Your password is: Pass456

This can happen because you are not associating the password with the username. To make them connected you would write the code like this, assuming that username and password are on separate lines:

SOLUTION:

while(line != null)
{
    Console.WriteLine(userInput == line);
    Console.WriteLine(password == line);

    if (userInput == line)
    {
    
// ** CHANGES BEGIN 
    
        line = reader.ReadLine(); // get password on next line
        if (line == password)
        {
            Console.WriteLine("Credentials are valid");
            
        }
        else 
        {
            Console.WriteLine("Credentials are invalid");
            
        }
        break; // no need to continue this loop
    }
    else {

        // Important: skip past password line since the username didnt match
        
        line = reader.ReadLine(); 
    }

// ** CHANGES END
    
    line = reader.ReadLine(); // this is now reading the next username or EOF
    
}

CodePudding user response:

Put the following into your text file:

hello:world

Put the following into your code:

    static void Main(string[] args)
    {
        string filepath = @"C:\myProgramingFiles\password.txt";

        Console.WriteLine("Write your username:");
        string username = Console.ReadLine();
        Console.WriteLine("Write your password:");
        string password = Console.ReadLine();

        var lines = File.ReadAllLines(filepath);

        var usrpwd = username   ":"   password;

        foreach(line in lines)
        {
            if(usrpwd == line)
                Console.WriteLine("Credentials accepted");
        }
    }

Run the app and type hello as the username and world as the password. Build from there ..

When it comes to saving new data in your text file look at File.AppendAllLines or read all your lines in, add new data in memory and then overwrite the whole file. That's probably most easily arranged by having the data in a list, rather than an array:

var lines = new List<string>(File.ReadAllLines(...));

Then you can lines.Add(...) a user:password pair and write it out again

  • Related