Random access file IO - what is wrong with this code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil

    #1

    Random access file IO - what is wrong with this code?

    Problem code:

    using System;
    using System.Collecti ons.Generic;
    using System.Text;
    using System.IO;
    using System.Diagnost ics;

    namespace Streams {
    class Program {
    static void Main(string[] args) {
    const string file = @"C:\Temp\file. txt";

    if (File.Exists(fi le))
    File.Delete(fil e);

    using (StreamWriter sw = new StreamWriter(fi le)) {
    sw.WriteLine("L ine 1");
    sw.WriteLine("L ine 2");
    sw.WriteLine("L ine 3");

    FileStream fs = sw.BaseStream as FileStream;
    Debug.Assert(fs != null);

    fs.Seek(0, SeekOrigin.Begi n);
    sw.WriteLine("L ine A");
    }
    }
    }
    }

    Expected result: a file with 3 lines
    Line A
    Line 2
    Line 3

    Actual result: a file with 4 lines
    Line 1
    Line 2
    Line 3
    Line A


    The Seek() is being ignored.

    Obviously I am doing something wrong - any hints guys?!


    --
    Phil
  • Michael

    #2
    Re: Random access file IO - what is wrong with this code?

    Hi,

    ..Flush() makes it work:

    sw.WriteLine("L ine 1");
    sw.WriteLine("L ine 2");
    sw.WriteLine("L ine 3");
    sw.Flush();
    FileStream fs = sw.BaseStream as FileStream;
    Debug.Assert(fs != null);
    fs.Seek(0, SeekOrigin.Begi n);
    sw.WriteLine("L ine A");
    sw.Flush();

    not sure it's the best solution...

    Mike


    "Phil" <foo@foo.comwro te in message
    news:a137e2l75i fs3s29b4tj0to9o afskjsbtv@4ax.c om...
    Problem code:
    >
    using System;
    using System.Collecti ons.Generic;
    using System.Text;
    using System.IO;
    using System.Diagnost ics;
    >
    namespace Streams {
    class Program {
    static void Main(string[] args) {
    const string file = @"C:\Temp\file. txt";
    >
    if (File.Exists(fi le))
    File.Delete(fil e);
    >
    using (StreamWriter sw = new StreamWriter(fi le)) {
    sw.WriteLine("L ine 1");
    sw.WriteLine("L ine 2");
    sw.WriteLine("L ine 3");
    >
    FileStream fs = sw.BaseStream as FileStream;
    Debug.Assert(fs != null);
    >
    fs.Seek(0, SeekOrigin.Begi n);
    sw.WriteLine("L ine A");
    }
    }
    }
    }
    >
    Expected result: a file with 3 lines
    Line A
    Line 2
    Line 3
    >
    Actual result: a file with 4 lines
    Line 1
    Line 2
    Line 3
    Line A
    >
    >
    The Seek() is being ignored.
    >
    Obviously I am doing something wrong - any hints guys?!
    >
    >
    --
    Phil

    Comment

    • Phil

      #3
      Re: Random access file IO - what is wrong with this code?

      On Thu, 17 Aug 2006 00:10:15 +0200, "Michael" <info@seeknsnat ch.com>
      wrote:
      >Hi,
      >
      >.Flush() makes it work:
      >
      >sw.WriteLine(" Line 1");
      >sw.WriteLine(" Line 2");
      >sw.WriteLine(" Line 3");
      >sw.Flush();
      >FileStream fs = sw.BaseStream as FileStream;
      >Debug.Assert(f s != null);
      >fs.Seek(0, SeekOrigin.Begi n);
      >sw.WriteLine(" Line A");
      >sw.Flush();
      >
      >not sure it's the best solution...
      >
      >Mike
      >http://www.seeknsnatch.com
      Thanks Michael I will give that a shot.
      --
      Phil

      Comment

      Working...