Demonstrates seeking to a position in a file from the end : File Read Write « File Stream « C# / C Sharp






Demonstrates seeking to a position in a file from the end

Demonstrates seeking to a position in a file from the end
  
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

// Seek.cs -- Demonstrates seeking to a position in a file from the end,
//            middle and beginning of a file
//
//            Compile this program with the following command line:
//                C:>csc Seek.cs
using System;
using System.IO;
using System.Text;

namespace nsStreams
{
    public class Seek
    {
        const string str1 = "Now is the time for all good men to " +
                            "come to the aid of their Teletype.\r\n";
        const string str2 = "The quick red fox jumps over the " +
                           "lazy brown dog.\r\n";
        static public void Main ()
        {
            FileStream strm;
            try
            {
                strm = new FileStream ("./StrmSeek.txt",
                                       FileMode.Create,
                                       FileAccess.ReadWrite);
            }
            catch (Exception e)
            {
                Console.WriteLine (e);
                Console.WriteLine ("Cannot open StrmSeek.txt " +
                                   "for reading and writing");
                return;
            }
// Clear out any remnants in the file
//            strm.SetLength (0);
            foreach (char ch in str1)
            {
                strm.WriteByte ((byte) ch);
            }
            foreach (char ch in str2)
            {
                strm.WriteByte ((byte) ch);
            }
// Seek from the beginning of the file
            strm.Seek (str1.Length, SeekOrigin.Begin);
// Read 17 bytes and write to the console.
            byte [] text = new byte [17];
            strm.Read (text, 0, text.Length);
            ShowText (text);
// Seek back 17 bytes and reread.
            strm.Seek (-17, SeekOrigin.Current);
            strm.Read (text, 0, text.Length);
            ShowText (text);
// Seek from the end of the file to the beginning of the second line.
            strm.Seek (-str2.Length, SeekOrigin.End);
            strm.Read (text, 0, text.Length);
            ShowText (text);
        }
        static void ShowText (byte [] text)
        {
            StringBuilder str = new StringBuilder (text.Length);
            foreach (byte b in text)
            {
    
                str.Append ((char) b);
            }
            Console.WriteLine (str);
        }
    }
}
//File: StrmSeek.txt

/*
Now is the time for all good men to come to the aid of their Teletype.
The quick red fox jumps over the lazy brown dog.

*/

           
         
    
  








Related examples in the same category

1.Demonstrates opening/creating a file for writing and truncating its length to 0 bytes.Demonstrates opening/creating a file for writing and truncating its length to 0 bytes.
2.Writes the same string to a file and to the screen using a common methodWrites the same string to a file and to the screen using a common method
3.Display a text file
4.Write to a file
5.Copy a file
6.Demonstrate random accessDemonstrate random access
7.Hex value Dump
8.Read all the content from a file as byte array
9.Read all the content from a file as string in default encoding
10.Gets a files' contents
11.Gets a files' contents with MemoryStream
12.Gets a files' contents from a Url
13.Gets a files' contents from a Url and save to an OutputStream
14.Gets a file's contents (Used primarily for text documents on an FTP)
15.Gets a files' contents from an Url with NetworkCredential
16.Read/Write File Transacted
17.Replace String In File
18.Append To File
19.Write To File
20.Writes out a string to a file.