rampuriyaaa
rampuriyaaa

Reputation: 5126

File Input/output issue

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

Upvotes: 0

Views: 110

Answers (4)

Swift
Swift

Reputation: 1881

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();                
          }
        }
      }

Upvotes: 0

Daniil Grankin
Daniil Grankin

Reputation: 3933

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'.

Upvotes: 0

Adriaan Stander
Adriaan Stander

Reputation: 166346

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());
}

Upvotes: 1

Spooky
Spooky

Reputation: 2993

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());
        }
    }
}

Upvotes: 1

Related Questions