qqqqqkks
qqqqqkks

Reputation: 247

How to add data to a file without overwriting previous data

I am trying to make a basic login and sign up c# console application, however, when i try making a new account it overwrites the previous account that was created.

Here is my code:

[Serializable]
        public class Users
        {
            public string UserName;
            public string Password;

            public Users(string userName, string password)
            {
                UserName = userName;
                Password = password;
            }
        }

public class SaveToFile
        {
            public static void SerializeSignUpDetails(string userName, string password)
            {
                Users obj = new Users(userName, password);
                IFormatter formatter = new BinaryFormatter();
                Stream stream = new FileStream("SignUp.txt", FileMode.Create, FileAccess.Write);
                formatter.Serialize(stream, obj);
                stream.Close();
            }
            public static Users DeserializeSignUpDetails()
            {
                Stream stream = new FileStream("SignUp.txt", FileMode.Open, FileAccess.Read);
                IFormatter formatter = new BinaryFormatter();
                Users objnew = (Users)formatter.Deserialize(stream);
                stream.Close();
                return objnew;
            }
        }

public static void Main(string[] args)
        {
            Console.WriteLine("To Login Type 1, To Create a new account Type 2");
            int LogInOrSignUp;
            do
            {
                int.TryParse(Console.ReadLine(), out LogInOrSignUp);
            } while (LogInOrSignUp != 1 && LogInOrSignUp != 2);

            string userName = "";
            string password = "";
            bool successfull = false;
            Users userDetails = SaveToFile.DeserializeSignUpDetails();
            while (!successfull)
            {
                if (LogInOrSignUp == 1)
                {
                    Console.WriteLine("Write your username:");
                    userName = Console.ReadLine();
                    Console.WriteLine("Enter your password:");
                    password = Console.ReadLine();
                    if (userName == userDetails.UserName && password == userDetails.Password)
                    {
                        Console.WriteLine("You have logged in successfully!");
                        successfull = true;
                        break;
                    }
                    if (!successfull)
                    {
                        Console.WriteLine("Your username or password is incorect, try again!");
                    }
                }

                else if (LogInOrSignUp == 2)
                {
                    Console.WriteLine("Enter a username:");
                    userName = Console.ReadLine();

                    Console.WriteLine("Enter a password:");
                    password = Console.ReadLine();

                    successfull = true;
                    SaveToFile.SerializeSignUpDetails(userName, password);
                }
            }
        }

UPDATE

thanks @DrkDeveloper for helping me solve my overwriting problem!

But now how do I loop through all the usernames and passwords in my file to check if there is a match to the username and password the user inputs when logging in?

Thanks for all the help so far!

Upvotes: 1

Views: 518

Answers (1)

DrkDeveloper
DrkDeveloper

Reputation: 949

In serialization stream use:

"FileMode.Append"

If you only Open or only Create you'll overwrite it.

Think about the fact if you append info in a file, you have to know how to read the data correctly, found where every append starts, etc... if you need it.

One more question, why are not using "using clauses"?

Edit:

public static void SerializeSignUpDetails(string userName, string password)
{
      Users obj = new Users(userName, password);
      IFormatter formatter = new BinaryFormatter();
      Stream stream = new FileStream("SignUp.txt", FileMode.Append, FileAccess.Write); //here is the point. you can use File.Append or File.Open static methods.
      formatter.Serialize(stream, obj);
      stream.Close();
}

Upvotes: 2

Related Questions