0

I read byte by byte in FileStream. I use ReadByte.

I always need to check next byte. I would like to be able, if I read a certain byte, to go back one byte.

The reason is that when I meet this byte, I need to pass the FileStream to another function, but it needs to read it at this specific previous position (back one byte).

How can I achieve this?

Indeed I searched https://www.bing.com/search?q=c%23+change+position+to+previous+stream+site%3astackoverflow.com but all questions suggest to use Seek(offset, Beginning). Some user suggested duplicate which shows how to use .Seek(0, SeekOrigin.Begin); - that definitely what I want. I need to seek to current position (for which I found plausible method be searching for "C# filestream position" - FileStream.Position) decreased by one.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Cherry
  • 37
  • 5
  • You have not shown any code so I will throw this out, are you using a `StreamReader`, if so is the `Peek()` method what you are looking for? – Simon Wilson May 26 '20 at 22:14
  • 3
    You may want to look up the [Seek method of filestream](https://learn.microsoft.com/en-us/dotnet/api/system.io.filestream.seek?view=netcore-3.1). Paired with [Position](https://learn.microsoft.com/en-us/dotnet/api/system.io.filestream.position?view=netcore-3.1) – Chrᴉz remembers Monica May 26 '20 at 22:15
  • Duplicate shows how to properly seek in a stream (using zero as an example essentially). If that is not enough (along with links provided by @ChrᴉzsaysreinstateMonica) - edit the question to show why. – Alexei Levenkov May 26 '20 at 22:22
  • The duplicate shows how to check of the stream is seekable and, if so, how to use the seek method. True, with the position 0, but that's not the question asked here. It's specifically pointed out here that it should be a specific position (current - 1 byte) and op could also not know how to get that position as the dupe doesn't cover that. – Chrᴉz remembers Monica May 26 '20 at 22:25
  • @ChrᴉzsaysreinstateMonica Why did you then suggested to use `Seek`? Clearly you thought that it is not an answer (as you've posted it as comment)... I'm very confused how using `Seek` is not an answer. I'm not sure if it would be appropriate to edit question to clarify that OP looked at the suggested duplciate and found Seek shown there to not suffice... – Alexei Levenkov May 26 '20 at 22:26
  • @ChrᴉzsaysreinstateMonica I've edited question to show what OP already tried and re-opened it. – Alexei Levenkov May 26 '20 at 22:45
  • @AlexeiLevenkov Thank you. I've been right at typing my answer when this question was closed as I had the same tought that my comment should be an answer. – Chrᴉz remembers Monica May 27 '20 at 06:26

1 Answers1

1

There is the Seek method to set the stream to a given position. You can get the current position of the stream with the Position property.

It should something like this then:

fileStream.Seek(filestream.Position - 1, SeekOrigin.Begin);
Chrᴉz remembers Monica
  • 1,829
  • 1
  • 10
  • 24