-1

I am trying to make a C# app that reads and outputs information from a .txt file and then allows the user to enter more information to the end of that file. I am trying to write the text file in CSV form, and I am having a lot of trouble figuring out how to add to the BOTTOM of the file. It seems that when I try, it overwrites the top line of the file. Any help is appreciated. Here is the code so far, sorry for any confusing lines-- I have been trying many different things i could find online to try to get it working.

class Program
{
    static void Main(string[] args)
    {
        string UIName = "";
        string UIInvoice = "";
        string UIDue = "";
        string UIAmount = "";

        using (FileStream fs = new FileStream(@"C:\Accounts.txt", FileMode.Open))
        using (StreamReader sr = new StreamReader(fs))
        {
            string content = sr.ReadToEnd();
            string[] lines = content.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            int lineCount = 0;
            List<Account> accounts = new List<Account>();
            foreach (string line in lines)
            {
                string[] column = line.Split(',');
                if (lineCount != 0)
                {
                    Account account = new Account();
                    account.AccountName = column[0];
                    account.InvoiceDate = column[1];
                    account.DueDate = column[2];
                    account.AmountDue = column[3];
                    accounts.Add(account);
                }
                lineCount++;
            }
            Console.WriteLine(content);
        }
        using (FileStream fs = new FileStream(@"C:\Accounts.txt", FileMode.OpenOrCreate))
        using (StreamWriter sw = new StreamWriter(fs))
        {
            Account account = new Account();
            account.AccountName = UIName;
            account.InvoiceDate = UIInvoice;
            account.DueDate = UIDue;
            account.AmountDue = UIAmount;
            //accounts.Add(account);
            string fullText = (UIName + "," + UIInvoice + "," + UIDue + "," + UIAmount);
                    Console.WriteLine("Would you like to enter additional data?");


            Console.WriteLine("Please enter the Account Name: ");
            UIName = Console.ReadLine();
            Console.WriteLine("Please enter the Invoice Date: ");
            UIInvoice = Console.ReadLine();
            Console.WriteLine("Please enter the Due Date: ");
            UIDue = Console.ReadLine();
            Console.WriteLine("Please enter the AmountDue: ");
            UIAmount = Console.ReadLine();

            File.AppendAllText("C:/Accounts.txt", fullText + Environment.NewLine);//can't get this way working, even after switching "\"s to "/"s. It says that the file is being used by another process.

            Console.ReadLine();
        }
    }
}
}

Separate class:

public class Account
{
    public string AccountName { get; set; }
    public string InvoiceDate { get; set; }
    public string DueDate { get; set; }
    public string AmountDue { get; set; }

    public static string GetAccountCSV(Account account)
    {
        string returnValue = account.AccountName + "," + account.InvoiceDate + "," + account.DueDate + "," + account.AmountDue;
        return returnValue;
    }
}

The .txt file says;

Account Name,Invoice Date,Due Date,Amount Due
Jane Doe,1/12/2017,2/12/2017,2000.00
Gonuts Inc,12/31/2017,2/28/2017,1566.50
Coppell
  • 1
  • 1
  • 2
  • 1
    Open file with Append mode. `new FileStream(@"C:\Accounts.txt", FileMode.OpenOrCreate)` should be changed to `new FileStream(@"C:\Accounts.txt", FileMode.Append)` – Chetan Jan 04 '18 at 06:24

2 Answers2

0

There are two issues with your code

  1. You have to open file in append mode @ref chetan comment
  2. You are creating fulltext string before reading from user input due to which its writing empty string to text file.

Please find working program class file below

 class Program
{
    static void Main(string[] args)
    {
        string UIName = "";
        string UIInvoice = "";
        string UIDue = "";
        string UIAmount = "";

        using (FileStream fs = new FileStream(@"C:/Accounts.txt", FileMode.Open))
        using (StreamReader sr = new StreamReader(fs))
        {
            string content = sr.ReadToEnd();
            string[] lines = content.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            int lineCount = 0;
            List<Account> accounts = new List<Account>();
            foreach (string line in lines)
            {
                string[] column = line.Split(',');
                if (lineCount != 0)
                {
                    Account account = new Account();
                    account.AccountName = column[0];
                    account.InvoiceDate = column[1];
                    account.DueDate = column[2];
                    account.AmountDue = column[3];
                    accounts.Add(account);
                }
                lineCount++;
            }
            Console.WriteLine(content);
        }
        using (FileStream fs = new FileStream(@"C:/Accounts.txt", FileMode.Append))
        using (StreamWriter sw = new StreamWriter(fs))
        {
            Account account = new Account();
            account.AccountName = UIName;
            account.InvoiceDate = UIInvoice;
            account.DueDate = UIDue;
            account.AmountDue = UIAmount;
            //accounts.Add(account);
            Console.WriteLine("Would you like to enter additional data?");


            Console.WriteLine("Please enter the Account Name: ");
            UIName = Console.ReadLine();
            Console.WriteLine("Please enter the Invoice Date: ");
            UIInvoice = Console.ReadLine();
            Console.WriteLine("Please enter the Due Date: ");
            UIDue = Console.ReadLine();
            Console.WriteLine("Please enter the AmountDue: ");
            UIAmount = Console.ReadLine();
            string fullText = (UIName + "," + UIInvoice + "," + UIDue + "," + UIAmount);
            File.AppendAllText("C:/Accounts.txt", fullText + Environment.NewLine);//can't get this way working, even after switching "\"s to "/"s. It says that the file is being used by another process.
            Console.ReadLine();
        }
    }
}
0

You have multiple issues with the code.

  1. You want to append the data to the end of the file but you are opening FileStream with OpenOrCreate mode. OpenOrCreate mode places file pointer at the start of the file. So after that whatever you write to the file it will overwrite the existing content of the file.

  2. You are opening the FileStream and StreamWriter but do not use them to write contents to the file. Instead of File.AppendAllText You should use sw.WriteLine(fullText).

Also you are writing the file content at the wrong place in the code.

Following is the code with all the above problems removed.

static void Main(string[] args)
    {
        string UIName = "";
        string UIInvoice = "";
        string UIDue = "";
        string UIAmount = "";
        var filePath = @"D:\Accounts.txt";
        using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
        {
            using (StreamReader sr = new StreamReader(fs))
            {
                string content = sr.ReadToEnd();
                string[] lines = content.Split(new string[] {Environment.NewLine},
                    StringSplitOptions.RemoveEmptyEntries);

                int lineCount = 0;
                List<Account> accounts = new List<Account>();
                foreach (string line in lines)
                {
                    string[] column = line.Split(',');
                    if (lineCount != 0)
                    {
                        Account account = new Account();
                        account.AccountName = column[0];
                        account.InvoiceDate = column[1];
                        account.DueDate = column[2];
                        account.AmountDue = column[3];
                        accounts.Add(account);
                    }
                    lineCount++;
                }
                Console.WriteLine(content);
            }
        }

        using (FileStream fs = new FileStream(filePath, FileMode.Append))
        {
            using (StreamWriter sw = new StreamWriter(fs))
            {
                Account account = new Account();
                account.AccountName = UIName;
                account.InvoiceDate = UIInvoice;
                account.DueDate = UIDue;
                account.AmountDue = UIAmount;
                //accounts.Add(account);

                Console.WriteLine("Would you like to enter additional data?");
                Console.WriteLine("Please enter the Account Name: ");
                UIName = Console.ReadLine();
                Console.WriteLine("Please enter the Invoice Date: ");
                UIInvoice = Console.ReadLine();
                Console.WriteLine("Please enter the Due Date: ");
                UIDue = Console.ReadLine();
                Console.WriteLine("Please enter the AmountDue: ");
                UIAmount = Console.ReadLine();

                string fullText = (UIName + "," + UIInvoice + "," + UIDue + "," + UIAmount);

                sw.WriteLine(fullText);

                Console.ReadLine();
            }
        }
    }
Chetan
  • 6,711
  • 3
  • 22
  • 32