Return StreamReader to Beginning
You need to seek on the stream, like you did, then call DiscardBufferedData
on the StreamReader
. Documentation here:
Edit: Adding code example:
Stream s = new MemoryStream();
StreamReader sr = new StreamReader(s);
// later... after we read stuff
s.Position = 0;
sr.DiscardBufferedData(); // reader now reading from position 0
Return StreamReader to Beginning when his BaseStream has BOM
//pass all 3 tests
void ResetStream(ref StreamReader sr){
sr.Read(); //ensure that BOM is detected if configured to do so
sr.BaseStream.Position=0;
sr=new StreamReader(sr.BaseStream, sr.CurrentEncoding, false);
}
Do I need to reset a stream(C#) back to the start?
When you read a stream to the end, specifically with StreamReader
's ReadToEnd
method, you have to Seek
it back to the beginning. This can be done like so:
StreamReader sr = new StreamReader(stream);
sr.ReadToEnd();
stream.Seek(0, SeekOrigin.Begin); //StreamReader doesn't have the Seek method, stream does.
sr.ReadToEnd(); // This now works
How to make the StreamReader read from the start of the textfile
This should be working, and I am guessing that there is something else happening along the way. Once you open a reader, it moves down and must be reset if you want it back to reading from the top. Maybe your reference was left open elsewhere (but that is assuming that the posted code has something happening that was not posted), or used before this piece of code?
However, you can always force the position to the top to verify this theory. If after this code, you still are at the 14th line, then it might be the file itself somehow?
reader.DiscardBufferedData();
reader.BaseStream.Seek(0, SeekOrigin.Begin);
reader.BaseStream.Position = 0;
c# - StreamReader and seeking
Yes you can, see this:
var sr = new StreamReader("test.txt");
sr.BaseStream.Seek(2, SeekOrigin.Begin); // Check sr.BaseStream.CanSeek first
Update:
Be aware that you can't necessarily use sr.BaseStream.Position
to anything useful because StreamReader
uses buffers so it will not reflect what you actually have read. I guess you gonna have problems finding the true position. Because you can't just count characters (different encodings and therefore character lengths). I think the best way is to work with FileStream
´s themselves.
Update:
Use the TGREER.myStreamReader
from here:
http://www.daniweb.com/software-development/csharp/threads/35078
this class adds BytesRead
etc. (works with ReadLine()
but apparently not with other reads methods)
and then you can do like this:
File.WriteAllText("test.txt", "1234\n56789");
long position = -1;
using (var sr = new myStreamReader("test.txt"))
{
Console.WriteLine(sr.ReadLine());
position = sr.BytesRead;
}
Console.WriteLine("Wait");
using (var sr = new myStreamReader("test.txt"))
{
sr.BaseStream.Seek(position, SeekOrigin.Begin);
Console.WriteLine(sr.ReadToEnd());
}
Can I return a StreamReader from a method?
Yes, of course. It's not a great idea, though - you're creating the StreamReader in one method, and closing it in another. It's better practice to create and close the reader in one method.
Strange question mark, when setting StreamReader to beginning
It's very likely that the file starts with a byte order mark (BOM) which is being ignored by the reader initially, but then not when you "rewind" the stream.
While you could create a new reader, or even just replace it after reading it, I think it would be better to just avoid reading the file twice to start with:
foreach (var question in File.ReadLines(text, Encoding.Unicode))
{
Console.WriteLine(question);
string response = Console.ReadLine();
potentialEmployee.Responses.Add(question, response);
}
That's shorter, simpler, more efficient code that also won't display the problem you asked about.
If you want to make sure you can read the whole file before asking any questions, that's easy too:
string[] questions = File.ReadAllLines(text, Encoding.Unicode);
foreach (var question in questions)
{
Console.WriteLine(question);
string response = Console.ReadLine();
potentialEmployee.Responses.Add(question, response);
}
Related Topics
Why Can't I Use System.Io.File Methods in an MVC Controller
Wpf: How to Override Part of a Controltemplate Without Redefining the Whole Style
How to Restrict/Control the Navigation Routes the User Can Visit Based on Login Status/Role
Outofmemoryexception While Populating Memorystream: 256Mb Allocation on 16Gb System
Zoom and Translate an Image from the Mouse Location
C# "Internal" Access Modifier When Doing Unit Testing
What Are the Benefits of Using C# VS F# or F# VS C#
Inno Setup for Windows Service
How to Easily Initialize a List of Tuples
What Really Happens in a Try { Return X; } Finally { X = Null; } Statement
Could Not Load File or Assembly ... the Parameter Is Incorrect
Fast Way to Convert a Two Dimensional Array to a List ( One Dimensional )
C# Validating Input for Textbox on Winforms
How to Use JSON.Net for JSON Modelbinding in an MVC5 Project