Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6931127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:32:12+00:00 2026-05-27T11:32:12+00:00

Im working on a little ATM program and I’m stuck on a StreamWritter problem.

  • 0

Im working on a little ATM program and I’m stuck on a StreamWritter problem.
On load, my program must use a StreamReader to read in 4 .txt files all located in my bin/debug. Then the user is asked to either Deposit or Withdraw money from the bank accounts located in the .txt files. Everything works fine for the StreamReader which loads all the bank accounts on program loading and for the StreamWriter to write the changes in the .txt files when I add/remove money.
My problem is when I close the program, the loading of the files works just fine but I can’t write in the files anymore. My StreamWriter jumps straight to the Catch part and cannot be instantiated. How is that possible if it worked just fine on first use.
Heres the StreamReader Code :

    public bool ReadSavingAccount()
    {
        string strLine;
        string[] strArray;
        char[] charArray = new char[] { ',' };
        FileStream aFile;
        StreamReader sr;
        try
        {
            aFile = new FileStream("mySavingAccount.txt", FileMode.Open);
            sr = new StreamReader(aFile);

            strLine = sr.ReadLine();
            while (strLine != null)
            {
                strArray = strLine.Split(charArray);
                Savings monSave = new Savings(strArray[0], Convert.ToDouble(strArray[1]));
                mySavingAccount.Add(monSave);
                strLine = sr.ReadLine();
            }
            sr.Close();
            aFile.Close();
        }
        catch
        {
            return false;
        }

        return true;
    }

And the StreamWrite Code:

    public bool WriteSavingAccount()
    {
        FileStream aFile;
        StreamWriter sw;
        string myString;

        try
        {
            aFile = new FileStream("mySavingAccount.txt", FileMode.Create);
            sw = new StreamWriter(aFile);
        }
        catch
        {
            return false;
        }

        foreach (Savings mySave in mySavingAccount)
        {
            myString = mySave.AccountNumber + "," + mySave.AccountBalance;
            sw.WriteLine(myString);

        }
        sw.Close();
        return true;
    }

Any idea what the problem could be ?
Thanks in advance and let me know if you need any other parts of the code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:32:12+00:00Added an answer on May 27, 2026 at 11:32 am

    Try adding a second parameter of boolean to your StreamWriter call. This is the append parameter. True to append to the file, false to overwrite it.

    sw = new StreamWriter(aFile, false);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a little file copying program for my company and it's
I'm working on a little program that needs to pipe binary streams very closely
I'm working on a little experimental utility to use within our company that indexes
I'm working on a little part of my program, handling the input, basically I
I'm working on a little program and would like to implement a menu in
I'm working on a little project, but I'm stuck and don't know what I'm
i am working on a little script grabbing some files from a website. First
I'm working on a little problem in my sparetime involving analysis of some images
I'm working on a little problem. I have <li> elements and I want to
I've been working a little with DevExpress CodeRush and Refactor! Pro this week, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.