Sam
Sam

Reputation: 1586

Stream confusion, understanding buffers

I'm writing my own XML and CSV parser in C# (for fun) and am having a little bit of trouble getting my streams to work. Basically I would like to load the file char by char and read it in that way. I would normally do readline, but that is just a bit too simple for what I am doing now and what I plan to do in the future. It does seem to work, but it is either really slow or working in an infinite loop. Output seems to be okay, but it takes a while.

I've been doing a lot of reading on MSDN to try and understand Streaming, but I am having trouble understanding the current stream position.

List<string> s = new List<string>();
StreamReader r = File.OpenText(f.FullName);
StreamWriter w = File.CreateText(@"C:\Users\XXXXX\Desktop\streamoutput.txt");
char[] buffer = new char[1024];
int count = 0;
string csvChunk = "";

while (r.Peek() >= 0) //Before end of file?
{
    r.Read(buffer, 0, buffer.length); //Attempting to load in 1024 characters

    foreach (char c in buffer)
    {
        if(c == ','){
            s.Add(csvChunk);
            csvChunk = "";
        }
        else
        {
            csvChunk += c;
            w.Write(c); //Write output to file (so I can see what is going on)
            count++;    //Number of chars done  
        }
    }
   Console.Clear();
   Console.WriteLine("Written " + count + " characters "); //Just to keep track of whats up

}
r.Close();
w.Close();

If you could clarify the following I would much appreciate it:

Upvotes: 1

Views: 125

Answers (2)

NValchev
NValchev

Reputation: 3005

Firstly, as @Leff said, you are using

csvChunk += c;

which is creating a new string object on each assignment as the string is an immutable object. You can use StringBuilder instead. Another thing which might improve your performance is BufferedStream.

var bufStream = new BufferedStream(<your stream reader>, buffer.Length);

Also, you don't need to check with the Peek method, the Read(...) method returns the total number of bytes read into the array, so your while statement would look:

while(bufStream.Read(buffer, 0, buffer.Length) != 0) 
{...}

On your second question: yes Third: if there are n bytes left, and n < buffer.Length, it reads the n bytes, put them into the buffer array, and returns n

Upvotes: 3

Leff
Leff

Reputation: 582

You should read more about c# strings, which are immutable. So, every time you do something like this

csvChunk += c;

you create new string object... for every character in your input file.

http://msdn.microsoft.com/en-us/library/362314fe.aspx

Upvotes: 0

Related Questions