streamreader

StreamReader, C#, peek

牧云@^-^@ 提交于 2019-12-06 12:07:00
问题 I have a StreamReader that once in a while check if it has more to read from a simple text file. It uses peek property. The problem is that when I am using peek the position is changed, althougth not suppose to. FileStream m_fsReader = new FileStream( m_strDataFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite ); StreamReader m_SR = new StreamReader(m_fsReader); Console.WriteLine("IfCanRead SR Position " + m_fsReader.Position + " and Length " + m_fsReader.Length); if (m_SR

StreamReader with tab delimited text file

时光总嘲笑我的痴心妄想 提交于 2019-12-06 06:39:33
I have a similar requirement to this post... Populate Gridview at runtime using textfile Where I want to read a text file with StreamReader and populate a DataTable with the data in the file, however I'm not sure how to implement a split() with a tab. Could anybody point me in the right direction, please? You can try this: DataTable table = new DataTable(); table.Columns.Add("col1"); table.Columns.Add("col2"); table.Columns.Add("col3"); var lines = File.ReadAllLines(@"Data.txt").ToList(); lines.ForEach(line => table.Rows.Add(line.Split((char)9))); I presumed that rows are delimited by newline

Read from StreamReader in batches

落花浮王杯 提交于 2019-12-05 16:57:45
I have been running into OutOfMemory Exceptions while trying to load an 800MB text file into a DataTable via StreamReader. I was wondering if there a way to load the DataTable from the memory stream in batches, ie, read the first 10,000 rows of the text file from StreamReader, create DataTable, do something with DataTable, then load the next 10,000 rows into the StreamReader and so on. My googles weren't very helpful here, but it seems like there should be an easy way to do this. Ultimately I will be writing the DataTables to an MS SQL db using SqlBulkCopy so if there is an easier approach

Consume a StreamReader stream .NET from a web request as it streams?

房东的猫 提交于 2019-12-05 15:15:12
I'm experimenting with the Twitter streaming api, and am trying to open a stream for a user to consume events as they happen. I'm using a standard set of classes for making REST api calls to twitter. When using https://userstream.twitter.com/2/user.json in a "GET" call the response stream never ends... I'm opening a StreamReader and reading the response as I would with any other REST call to Twitter. This is probably obvious to others, but how do I "consume" this stream... Is there a way to read the StreamReader as it's reading (meaning before it closes)? or maybe there a different method I

StreamReader and binary data

被刻印的时光 ゝ 提交于 2019-12-05 08:06:54
I have this text file what contains different fields. Some fields may contain binary data. I need to get all the data in the file but right now when using StreamReader then it wont read the binary data block and data what comes after that. What would be the best solution to solve this problem? Example: field1|field2|some binary data here|field3 Right now i read in the file like this: public static string _fileToBuffer(string Filename) { if (!File.Exists(Filename)) throw new ArgumentNullException(Filename, "Template file does not exist"); StreamReader reader = new StreamReader(Filename,

How to take a stringbuilder and convert it to a streamReader?

感情迁移 提交于 2019-12-05 05:47:39
How to take a stringbuilder and convert it to a stream? SO my stringbuilder has to be converted into a : StreamReader stream = ???? Update I tried using a stringreader like: StringReader sr = new StringReader(sb.ToString()); StreamReader stream = new StreamReader(sr); but that doesn't work? Use ToString to convert the StringBuilder into a String, and use a StringReader to wrap the String as a Stream. If using a StreamReader is a requirement then convert the string to a memory stream and then create a new StreamReader using that object: StreamReader reader= new StreamReader( new MemoryStream

Why disposing StreamReader makes a stream unreadable? [duplicate]

本秂侑毒 提交于 2019-12-05 01:42:46
This question already has answers here : Can you keep a StreamReader from disposing the underlying stream? (8 answers) Closed 4 years ago . I need to read a stream two times, from start to end. But the following code throws an ObjectDisposedException: Cannot access a closed file exception. string fileToReadPath = @"<path here>"; using (FileStream fs = new FileStream(fileToReadPath, FileMode.Open)) { using (StreamReader reader = new StreamReader(fs)) { string text = reader.ReadToEnd(); Console.WriteLine(text); } fs.Seek(0, SeekOrigin.Begin); // ObjectDisposedException thrown. using

Override StreamReader's ReadLine method

寵の児 提交于 2019-12-04 19:59:58
I'm trying to override a StreamReader's ReadLine method, but having difficulty doing so due to inability to access some private variables. Is this possible, or should I just write my own StreamReader class? Assuming you want your custom StreamReader to be usable anywhere that a TextReader can be used there are typically two options. Inherit from StreamReader and override the functions that you want to have work differently. In your case this would be StreamReader.ReadLine. Inherit from TextReader and implement the reader functionality completely to your requirements. NB: For option 2 above,

How to read byte[] with current encoding using streamreader

 ̄綄美尐妖づ 提交于 2019-12-04 18:35:47
I would like to read byte[] using C# with the current encoding of the file. As written in MSDN the default encoding will be UTF-8 when the constructor has no encoding: var reader = new StreamReader(new MemoryStream(data)). I have also tried this, but still get the file as UTF-8: var reader = new StreamReader(new MemoryStream(data),true) I need to read the byte[] with the current encoding. A file has no encoding. A byte array has no encoding. A byte has no encoding. Encoding is something that transforms bytes to text and vice versa. What you see in text editors and the like is actually program

Reading the next line of a file only once

。_饼干妹妹 提交于 2019-12-04 14:01:57
I have an application that reads information from a text file and then categorizes them and puts them onto a Database. For one category, I need to check the line that comes right after the current line and look for a certain keyword? How do i get to read this line? This should happen when the streamreader has the current line already open.... I'm using c# on VS2010. Edit : All of the code below is in a while (!sReader.EndOfStream) loop string line = sReader.ReadLine(); //Note: this is used way above and lots of things are done before we come to this loop for (int i = 0; i < filter_length; i++)