streamreader

HTTPWebResponse + StreamReader Very Slow

Deadly 提交于 2019-11-27 06:45:55
I'm trying to implement a limited web crawler in C# (for a few hundred sites only) using HttpWebResponse.GetResponse() and Streamreader.ReadToEnd() , also tried using StreamReader.Read() and a loop to build my HTML string. I'm only downloading pages which are about 5-10K. It's all very slow! For example, the average GetResponse() time is about half a second, while the average StreamREader.ReadToEnd() time is about 5 seconds! All sites should be very fast, as they are very close to my location, and have fast servers. (in Explorer takes practically nothing to D/L) and I am not using any proxy.

.NET C# - Random access in text files - no easy way?

≡放荡痞女 提交于 2019-11-27 05:40:49
问题 I've got a text file that contains several 'records' inside of it. Each record contains a name and a collection of numbers as data. I'm trying to build a class that will read through the file, present only the names of all the records, and then allow the user to select which record data he/she wants. The first time I go through the file, I only read header names, but I can keep track of the 'position' in the file where the header is. I need random access to the text file to seek to the

Difference between StreamReader.Read and StreamReader.ReadBlock

隐身守侯 提交于 2019-11-27 03:03:48
问题 The documentation simply says ReadBlock is "a blocking version of Read" but what does that mean? Someone else has asked the question before but, huh? http://www.pcreview.co.uk/forums/thread-1385785.php The guy answering said Basically, it means that you can rely on StreamReader.ReadBlock not returning until either it's read as much as you've asked it to, or it's reached the end of the stream. Am I understanding correctly that this is required because Read may not give you everything you asked

C# - StreamReader.ReadLine does not work properly!

梦想与她 提交于 2019-11-27 02:52:10
问题 Simply I have been trying to implement what BufferedStreamReader does in Java. I have a socket stream open and just want to read it in a line-oriented fashion - line by line. I have the following server-code. while (continueProcess) { try { StreamReader reader = new StreamReader(Socket.GetStream(), Encoding.UTF8); string command = reader.ReadLine(); if (command == null) break; OnClientExecute(command); } catch (Exception e) { Console.WriteLine(e.ToString()); } } And the following client-code:

How to count lines fast?

一笑奈何 提交于 2019-11-27 02:31:36
问题 I tried unxutils' wc -l but it crashed for 1GB files. I tried this C# code long count = 0; using (StreamReader r = new StreamReader(f)) { string line; while ((line = r.ReadLine()) != null) { count++; } } return count; It reads a 500MB file in 4 seconds var size = 256; var bytes = new byte[size]; var count = 0; byte query = Convert.ToByte('\n'); using (var stream = File.OpenRead(file)) { int many; do { many = stream.Read(bytes, 0, size); count += bytes.Where(a => a == query).Count(); } while

search text file using c# and display the line number and the complete line that contains the search keyword

不羁的心 提交于 2019-11-27 02:04:30
I require help to search a text file (log file) using c# and display the line number and the complete line that contains the search keyword. This is a slight modification from: http://msdn.microsoft.com/en-us/library/aa287535%28VS.71%29.aspx int counter = 0; string line; // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.txt"); while((line = file.ReadLine()) != null) { if ( line.Contains("word") ) { Console.WriteLine (counter.ToString() + ": " + line); } counter++; } file.Close(); Bit late to the game on this one, but happened

An elegant way to consume (all bytes of a) BinaryReader?

北城以北 提交于 2019-11-27 01:47:28
Is there an elegant to emulate the StreamReader.ReadToEnd method with BinaryReader ? Perhaps to put all the bytes into a byte array? I do this: read1.ReadBytes((int)read1.BaseStream.Length); ...but there must be a better way. Scott Rippey Simply do: byte[] allData = read1.ReadBytes(int.MaxValue); The documentation says that it will read all bytes until the end of the stream is reached. Update Although this seems elegant, and the documentation seems to indicate that this would work, the actual implementation (checked in .NET 2, 3.5, and 4) allocates a full-size byte array for the data, which

StreamReader and seeking

╄→尐↘猪︶ㄣ 提交于 2019-11-26 22:46:01
can you use streamreader to read a normal textfile and then in the middle of reading close the streamreader after saving the current position and then open streamreader again and start reading from that poision ? if not what else can i use to accomplish the same case without locking the file ? something like this: var fs = File.Open(@"C:\testfile.txt", FileMode.Open, FileAccess.Read); var sr = new StreamReader(fs); Debug.WriteLine(sr.ReadLine());//Prints:firstline var pos = fs.Position; while (!sr.EndOfStream) { Debug.WriteLine(sr.ReadLine()); } fs.Seek(pos, SeekOrigin.Begin); Debug.WriteLine

Error opening streamreader because file is used by another process

萝らか妹 提交于 2019-11-26 22:12:33
问题 I am developing an application to read an excel spreadsheet, validate the data and then map it to a sql table. The process is to read the file via a streamreader, validate the data, manually make corrections to the excel spreadsheet, validate again -- repeat this process until all data validates. If the excel spreadsheet is open, then when I attempt to read the data via a streamreader I get an error, "The process cannot access the file ... because it is being used by another process." Is

Tracking the position of the line of a streamreader

▼魔方 西西 提交于 2019-11-26 21:18:41
问题 Hi guys what i need to do is track the position of the line that I am reading from the stream reader when I say reader.ReadLine() i need to know the position of that line in the file and I also want to be able to then read the file from the position i have previously tracked. Is this possible? If so please assist. Help is much appreciated Thanks in advance. 回答1: You can do this one of three ways: 1) Write your own StreamReader. Here's a good place to start: How to know position(linenumber) of