memorystream question - c#

After write the xml document into the memory stream. When I want to use it by using XMLDocuments.Load, I have to set the position back to 0.
I am wondering If there any standard way to do it?

Well the simplest way is just:
stream.Position = 0;
I'm not sure what you're after beyond that. You can use the Seek method, but personally I find the Position property to be far simpler.
Do you definitely need to go via a stream in the first place? If you've already got the XmlDocument, why not just use that?

That's pretty much how you have to do it. The position must be set back to 0, because after writing the document into the stream, the stream is positioned at the end, ready to append more data. Setting the position to 0 effectively "rewinds" the stream, so that you will read it back in from the beginning.
This is a normal and expected usage pattern, if you are doing something like this anyway.

Related

How to move cursor back 1 reading with FileStream?

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.
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);

Possible to determine if a stream is a string?

Is it possible to somehow (without a huge performance loss) determine if a Stream (just a normal System.IO.Stream) "contains" a string or not? I have tried to google this, but I havent found a good solution that doesnt involve try and catching.
Any stream can be a string as a string is just a series of bytes. If you're asking if a stream contains a specific sequence of bytes -- i.e. you want to confirm that a stream contains a $MY_TOKEN$ somewhere then you'll have to read up until that point or to the end of the stream.
Depending on the nature of your stream, there might be an efficient way to do this and then reset the cursor back to the beginning of the stream.

What does "Position in a stream" mean in C#?

I have trouble to understand the word "Position of a stream". My question is somehow related to the concept of the stream method Seek(); it is kind confusing to me what this method does, they say its purpose is to set the position of the stream to a given value but yet its name describes the seek operation not set operation. Does anyone understand clearly what these two words are for and how they work together? Thanx
Think about a file as a sequence of bytes, and a stream as a view over that sequence, with a cursor marking the current position - so as you read data, the cursor is advanced. The Position property is simply the position of that cursor. So when you open a stream it's typically at 0, and as you read it increases. For seekable streams, you can "rewind" to the start of the stream with
stream.Position = 0;
or maybe skip 10 bytes using:
stream.Position += 10;
etc.
A stream is basically a sequence of bytes - the position is the point in the sequence that the stream is in.
The point of Seek is to "jump" to a location in the stream - a specific index (aka Position), similar to seeks done in a hard drive. With Seek you can specify an offset to start seeking from, so how many bytes to "jump".
The following 2 statements do exactly the same:
s.Position = 100;
s.Seek(100, SeekOrigin.Begin);
And they both determine the position (as a bytecount) where the next Read or Write will occur.
The Seek() name is very ancient.
I actually agree that, at first glance, Seek is not the best name for what it does. SeekAndSet or SeekAndMove would make more sense to me because that is what the method does - it seeks the position you want in the stream and sets the cursor to that position.
However, when you think of Seek in Computer Science terms in relation to hard disk drives it becomes obvious what this method does. It seeks and moves to the position.

XNA StreamReader/Writer how to read/write in different lines?

I'm trying to write a simple .txt via StreamWriter. I want it to look like this:
12
26
100
So simple numbers. But how do I tell the Reader/writer in which line to write or read.
http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx
Here it says that ReadLine() reads a line of current the Stream. But how do I know which line it is. Or is it always the first one?
I want to read the numbers, modify them and then write them back.
Any suggestions?
Thanks in advance!
A reader is conceptually a unidirectional thing, from the start (or more accurately, the current position in the stream) to the end.
Each time you read a line, it is simply buffering data until it finds a new line; it doesn't really have the concept of a current line (or moving between lines).
As long as the file isn't massive, you should be OK reading the entire file, working on a string (or string array), then saving the entire file; inserting/removing text content is otherwise non-trivial (especially when you consider the mysteries of encodings).
File.ReadAllLines and File.WriteAllLines may be easier in your scenario.
I think the only way is to read all lines in array (or any other data structude, i.e. list), modify and write it back to file.
Maybe xml will be better for your purposes?

Streams Why Use Seek(0L, SeekOrigin.Begin) instead of Position = 0 or vice versa

Could anyone please explain to me the differences, if any?
I tried to Google it but couldn't find much information. Maybe I didn't use correct keywords.
Any insight would be greatly appreciated.
stream.Seek(x, SeekOrigin.Begin); and stream.Position = x; both result in the stream position being set to x. The difference is that the Position setter unconditionally discards any read buffer, while the Seek method attempts to retain the part of the buffer that is still relevant to the new position.
You'll have to test, which one is faster for your scenario, but there's definitely a performance difference and neither is faster in all cases. I really wonder why this difference isn't documented.
In your example there is no difference.
The actual difference between Stream.Position and Stream.Seek is that Position uses an absolute offset whereas Seek uses an offset relative to the origin specified by the second argument.
As far as I can tell, at least for this specific case, nothing.
Both method Seek() and property Position require CanSeek to be true so from what I see it's up to the implementer.
Seek is really there to allow searching from specified locations (SeekOrigins) to an offset (the examples given on MSDN are somewhat convoluted but representative of the purpose: http://msdn.microsoft.com/en-us/library/system.io.filestream.seek.aspx).
Position is absolute and is obviously not meant for searching.
The case you mentioned just happens to be equivalent.
Personally, I'd use .Position = 0 to move to the beginning of the stream as that reads cleaner to me than "Seek using the beginning of the file as an origin and move this 0 offset of bytes."

Categories

Resources