开发者

Read file from position

开发者 https://www.devze.com 2023-04-09 22:47 出处:网络
FileStream infile = new FileStream(@\"C:\\Users\\John\\Desktop\\ProjectNew\\开发者_开发百科nov.txt\",FileMode.Open, FileAccess.Read);
FileStream infile = new FileStream(@"C:\Users\John\Desktop\ProjectNew\开发者_开发百科nov.txt",     FileMode.Open, FileAccess.Read);
        int position = x.Length;
        infile.Seek(position, SeekOrigin.Begin);

But Seek method returns number. How to read the file 'infile' from position to end in a string?


Is this what you're after? Assuming you wanted to start reading from position 100...

       using (FileStream fs = new FileStream(@"file.txt", FileMode.Open, FileAccess.Read))
        {
            fs.Seek(100, SeekOrigin.Begin);

            byte[] b = new byte[fs.Length - 100];
            fs.Read(b, 0, (int)(fs.Length - 100));

            string s = System.Text.Encoding.UTF8.GetString(b);
        }


The Seek method is supposed to return a number, the new position in the stream. Now just call whatever Read function you want.


Seek() only places the file pointer somewhere else. If you do a read before the seek, it will read from the beginning of the file. If you read after the seek, it will start reading from position.

So to read the file from position to the end, do Seek(), followed by Read() or ReadToEnd().

0

精彩评论

暂无评论...
验证码 换一张
取 消