0
int AccNum;
FileStream myfile = new FileStream("C:\\bankin.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamReader rd = new StreamReader(myfile);
StreamWriter wt = new StreamWriter(myfile);
int a = Convert.ToInt32(rd.ReadLine());
AccNum = a;
a += 1;
wt.WriteLine(Convert.ToString(a));
Console.WriteLine(rd.ReadLine());
rd.Close(); 
wt.Close();
myfile.Close();

I am trying to increment an integer value in the file banking.txt, but I am getting the following error:

Cannot access a closed file

1
  • On which line is the error being thrown? Commented Jul 19, 2013 at 8:04

4 Answers 4

1

Perhaps it's because you're closing rd before wt?

If that is the case, I would recommend using the using statement to prevent this confusion in the future:

int AccNum;
using (FileStream myfile = new FileStream("C:\\bankin.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
    using (StreamReader rd = new StreamReader(myfile)) {
        using (StreamWriter wt = new StreamWriter(myfile)) {
            int a = Convert.ToInt32(rd.ReadLine());
            AccNum = a;
            a += 1;
            wt.WriteLine(Convert.ToString(a));
            Console.WriteLine(rd.ReadLine());
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Change your code to make use of the using statements

Provides a convenient syntax that ensures the correct use of IDisposable objects.

int AccNum;
using(FileStream myfile = new FileStream("C:\\bankin.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite))
using(StreamReader rd = new StreamReader(myfile))
using (StreamWriter wt = new StreamWriter(myfile))
{
    int a = Convert.ToInt32(rd.ReadLine());
    AccNum = a;
    a += 1;
    wt.WriteLine(Convert.ToString(a));
    Console.WriteLine(rd.ReadLine());
}

Comments

0
int AccNum;
using(FileStream myfile = new FileStream("C:\\bankin.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
    using(StreamReader rd = new StreamReader(myfile))
    {
        using(StreamWriter wt = new StreamWriter(myfile))
        {
            int a = Convert.ToInt32(rd.ReadLine());
            AccNum = a;
            a += 1;
            wt.WriteLine(Convert.ToString(a));
        }
        Console.WriteLine(rd.ReadLine());
    }
}

It's good practice to use 'using'.

Comments

0

the exception is produced by the line wt.Close() because the file is already closed. the Close method on StreamReader close stream and all the underlying resources (see http://msdn.microsoft.com/en-us/library/system.io.streamreader.close.aspx)

and I assume that you want to save changes, so use Flush to save or use Close with AutoFlush in place of Flush. here is your example with some modification

int AccNum;
      using (FileStream myfile = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite))
      {
        using (StreamReader rd = new StreamReader(myfile))
        {
          using (StreamWriter wt = new StreamWriter(myfile))
          {             
            int a = Convert.ToInt32(rd.ReadLine());
            AccNum = a;
            a += 1;
            wt.WriteLine(Convert.ToString(a));
            Console.WriteLine(rd.ReadLine());
            wt.Flush();                
          }
        }
      }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.