Reading a fixed length random access text file in 2003


  • I know this is old stuff but one of our systems outputs it's data in a fixed field length text file.  It's a very old system and they are not planning to upgrade if to several months.  The nice people from this forum helped me get the Structure right for 2003.

    Now what I'm having a problem with is opening the file to read the data.  I had it setup in VB6 but is a whole new monster and I'm still learning it. 

    Does anyone have a tiny bit of code that could get me on the right path?

    How do you convert:

    Dim Order(10) As OrderHeader
    Open DataPath & "mlhdr" For Random As #iFile1 Len = 770

    to and once it's opened.  What replaces the Get, Put and Input commands from VB6?

    Once again... thanks in advance.

    Wednesday, November 16, 2005 12:42 PM


  • Use the filestream object:

    Public Sub New(ByVal path As
    String, ByVal mode As System.IO.FileMode, ByVal access As System.IO.FileAccess, ByVal share As System.IO.FileShare, ByVal bufferSize As Integer, ByVal useAsync As Boolean)

    Member of: System.IO.FileStream


    Initializes a new instance of the System.IO.FileStream class with the specified path, creation mode, read/write and sharing permission, buffer size, and synchronous or asynchronous state.


    bufferSize: A positive System.Int32 value greater than 0 indicating the buffer size. For bufferSize values between zero and eight, the actual buffer size is set to eight bytes.

    share: A System.IO.FileShare constant that determines how the file will be shared by processes.

    mode: A System.IO.FileMode constant that determines how to open or create the file.

    useAsync: Specifies whether to use asynchronous I/O or synchronous I/O. However, note that the underlying operating system might not support asynchronous I/O, so when specifying true, the handle might be opened synchronously depending on the platform. When opened asynchronously, the System.IO.FileStream.BeginRead(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object) and System.IO.FileStream.BeginWrite(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object) methods perform better on large reads or writes, but they might be much slower for small reads or writes. If the application is designed to take advantage of asynchronous I/O, set the useAsync parameter to true. Using asynchronous I/O correctly can speed up applications by as much as a factor of 10, but using it without redesigning the application for asynchronous I/O can decrease performance by as much as a factor of 10.

    access: A System.IO.FileAccess constant that determines how the file can be accessed by the FileStream object. This gets the System.IO.FileStream.CanRead and System.IO.FileStream.CanWrite properties of the FileStream object. System.IO.FileStream.CanSeek is true if path specifies a disk file.

    path: A relative or absolute path for the file that the current FileStream object will encapsulate.


    System.IO.DirectoryNotFoundException: The specified path is invalid, such as being on an unmapped drive.

    System.ArgumentNullException: path is null.

    System.ArgumentOutOfRangeException: bufferSize is negative or zero.-or- mode, access, or share contain an invalid value.

    System.Security.SecurityException: The caller does not have the required permission.

    System.ArgumentException: path is an empty string (""), contains only white space, or contains one or more invalid characters.

    System.IO.FileNotFoundException: The file cannot be found, such as when mode is FileMode.Truncate or FileMode.Open, and the file specified by path does not exist. The file must already exist in these modes.

    System.IO.IOException: An I/O error occurs, such as specifying FileMode.CreateNew and the file specified by path already exists. The system is running Windows 98 or Windows 98 Second Edition and share is set to FileShare.Delete.

    System.IO.PathTooLongException: The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

    System.UnauthorizedAccessException: The access requested is not permitted by the operating system for the specified path, such as when access is Write or ReadWrite and the file or directory is set for read-only access.

    Other Methods:  Read, ReadByte, Write, WriteByte, and Position

    Wednesday, November 16, 2005 2:27 PM