简体   繁体   中英

How to overwrite or skip specific lines when copying to another text file

I have a program in which a user can set up a bank account. It writes (in order) name, address, balance, and account type to a base text file. I am trying to provide a function for the user to be able to update their account by providing a new name and address. The balance and account type cannot be edited, and are taken from the base file.

The program currently writes the new data to a temporary file, which will eventually overwrite the base file with its contents. However, it also writes the old data from the user to the file, which is not what I want. Currently, the file will contain both the old data (before the user went to change it) and the new data (what the user entered to update). I would like some help on how I can overwrite or skip the old data in the temporary file, and leave only the new data and other accounts.

I have tried to implement this feature by using the String.Replace method, iterating it for each data item by using a for loop (I use a for loop to show the user's data if they select the option to view their data, which works for that), but it did not function as intended (it did not replace the text or do anything).

 else if (ChoiceInput == "3")
                //Choice 3 - Update account details.
                {
                    //Check that the account exists.

                    using (FileStream fs2 = new FileStream(FileName, FileMode.Open, FileAccess.ReadWrite))
                    {
                        StreamReader sr2 = new StreamReader(fs2);
                        StreamWriter sw2 = new StreamWriter(fs2);
                            Console.Write("Enter the account name: ");
                            string NameCheck = Console.ReadLine();
                            string NameValidation;
                        try
                        {
                            NameValidation = sr2.ReadLine();
                            string NameReplace = NameValidation;
                            NameReplace.Replace(NameValidation, "");
                            sw2.Write(NameReplace);
                            while (NameValidation != null)
                                {
                                    if (NameValidation.Contains(NameCheck))
                                    //Account exists.
                                    {
                                        for (int i = 0; i < 3; i++)
                                        {
                                        if (i == 1)
                                        {
                                            string ReadReplace = sr2.ReadLine();
                                            ReadReplace.Replace(ReadReplace, "" + "\r\n");
                                            sw2.Write(NameReplace);
                                            Console.WriteLine(ReadReplace);
                                        }
                                        else if (i == 2)
                                        {
                                            string ReadReplace = sr2.ReadLine();
                                            ReadReplace.Replace(ReadReplace, "" + "\r\n");
                                            sw2.Write(NameReplace);
                                            Console.WriteLine(ReadReplace);
                                        }
                                        }
                                    }
                                using (StreamWriter sw = new StreamWriter(FileNameTemp, true))
                                    {
                                    //Enter new details. It writes these details to the temporary file.
                                    Console.WriteLine("Account found.");
                                        Console.WriteLine("Enter new name: ");
                                        string AccountName = Console.ReadLine();
                                        sw.WriteLine(AccountName);
                                        Console.WriteLine("Enter new address: ");
                                        string AccountAddress = Console.ReadLine();
                                        sw.WriteLine(AccountAddress);
                                        NameValidation = null;
                                        for (int i = 0; i < 3; i++)
                                        //Loop that iterates twice, writing the balance and account type to the temporary file. This data cannot be changed.
                                        {
                                            string Write = sr2.ReadLine();
                                            if (i == 1)
                                            //Balance.
                                            {
                                                sw.WriteLine(Write);
                                            }
                                            else if (i == 2)
                                            //Account type.
                                            {
                                                sw.WriteLine(Write);
                                            }
                                        }
                                    }
                                    using (StreamReader sre = new StreamReader(FileName, true))
                                    {
                                        using (StreamWriter sw = new StreamWriter(FileNameTemp, true))
                                        {
                                            string Lines;
                                            int Counter = 0;
                                            while ((Lines = sre.ReadLine()) != null)
                                            //While the program still has lines to go through, it will continue writing to the text file.
                                            {
                                                Console.WriteLine(Lines);
                                                sw.Write(Lines + "\r\n");
                                                Counter++;
                                            }
                                        }
                                        using (FileStream fs = new FileStream(FileNameTemp, FileMode.Truncate))
                                        //Clears the temporary file, as not doing so would cause issues.
                                        {
                                        }
                                        Console.WriteLine("Account details updated!");
                                    }
                                }
                        }
                        finally
                        {
                            Console.ReadLine();
                        }
                        }
                    }

Update: I have added an example of what I am trying to do. The first for loop under NameValidation is supposed to remove the old details on the base file by replacing them with empty strings. However, it does not work as intended. It does not write anything to the base file, and then it crashes with the error "System.IO.IOException: 'The process cannot access the file 'C:\\program1\\Bank.txt' because it is being used by another process.'", when it carries onto StreamReader sre. However, it does write the balance and account type of the next account into the temporary file.

Okay. Some tips on how to proceed.

This is how your text file should be

Id, Name, address, Balance
1, testName, xyz, 300

Before Insert check if the name already exists. If yes, get the id (here if testname exists, we have to get the id value ie 1).

If we get this id, we can parse the file and update them with StreamWriter

[Or]

Read the name and address, replace with string.replace

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM