CdrTomalak
CdrTomalak

Reputation: 479

StreamWriter constructor problems

I am trying to do something very simple.

In finding the default buffersize associated with the StreamWriter(string path) constructor too small for the information I am logging, I am attempting to use the following constructor:

public StreamWriter(
    string path,
    bool append,
    Encoding encoding,
    int bufferSize
)

The resulting log file is completely empty - which is worse.

NOTE: My original post cited the error "Error: Attempted to read past end of stream" , but this relates to functionality later in the method, which I can't log information for because of this log file problem.

Here is the old constructor usage from my code:

drawGameBoardLog = new StreamWriter(debugFileName);

And here is the new constructor which ironically makes things worse:

drawGameBoardLog = new StreamWriter(debugFileName, false, System.Text.Encoding.Default, 65535);

I am totally baffled by this.

UPDATE: Some more detail:

This is the start of the method for which I am logging activity:

    public void DrawGameBoard()
    {
        StreamWriter drawGameBoardLog;
        bool debug = true;

        // Logging---------------------------------------------------------------------------------
        // Build timestamp string
        DateTime currentDateTime = DateTime.Now;
        string timeStampString = currentDateTime.ToString("ddMMyyyy_HHmmss");

        // Build filename, concat timestamp and .txt extension.
        string debugFileName = "D:\\Programming\\PacmanLogs\\DrawGameBoard"+timeStampString+".txt";

        // Create filestream and pass this to a stream writer, setting a nice big buffer.
        drawGameBoardLog = new StreamWriter(debugFileName, false, System.Text.Encoding.Default, 65535);
        //drawGameBoardLog = new StreamWriter(debugFileName);

        // Write to the file:
        drawGameBoardLog.WriteLine(DateTime.Now);
        drawGameBoardLog.WriteLine("timeStampString = {0}",timeStampString);
        // -------------------------------------------------------------------------------------------

        if(debug){drawGameBoardLog.WriteLine("DrawGameBoard()...");}

The line "DrawGameBoard()..." is not even appearing when using the StreamWriter constructor which accepts path, append, encoding, and buffersize. Whereas before I was getting content which took the file size to 1K. Here is the log file up to that point (I'm starting out with graphics programming by the way):

19/08/2012 14:13:21
timeStampString = 19082012_141321
DrawGameBoard()...
noOfIndexes = [6], noOfVertices = [4]
...just set stremSize = [80]
...creating vertices DataStream...
...building Matrices...
...Scaling matrix DONE...
...Rotation matrix DONE...
...Translation matrix DONE...
...Orthogonal matrix DONE...
...Perspective matrix DONE...
...COMBINED matrix DONE...
...creating Vector3 and Vector2 arrays to hold positions and texture coords...
...Writing Texture coords....
...Building Index Array (order of vertices to be drawn for a quad)....
...Declaring indicesStream. Set size of stream to [24]....
...Created data stream for indices OK. Now writing indices array to it....
...DONE. Just set position back to ZERO...
...Created new index buffer OK....
...configure the Input Assembler....
...Getting Vectors for position [0,0]...
...Got Vectors into myVectorPositions....
myVectorPositions[0] = [X:0 Y:0 Z:0.5]
myVectorPositions[1] = [X:20 Y:0 Z:0.5]
myVectorPositions[2] = [X:0 Y:20 Z:0.5]

The default buffer size kicks in right there.

Upvotes: 0

Views: 1960

Answers (2)

Steve
Steve

Reputation: 216273

Please change this line

 drawGameBoardLog = new StreamWriter(debugFileName, false, 
                      System.Text.Encoding.Default, 65535); 

in

 using(StreamWriter drawGameBoardLog = new StreamWriter(debugFileName, false, 
                      System.Text.Encoding.Default, 65535))
 {


  .... write all the stuff in your log

 }  

The using statement block will ensure that the streamwriter will be closed and written to disk

Probably, your first attempt using a StreamWriter without buffer worked partially, but, when you change the buffer to a big value, then only when you reach that dimension the file is written to disk.

Upvotes: 1

Thanatos
Thanatos

Reputation: 1186

You are not closing your StreamWriter. At the end type:

drawGameBoardLog.Close();

Or use a using block:

using(StreamWriter sw = new StreamWriter(path))
{
     sw.WriteLine("Sth");
}

Or use a try finally:

StreamWriter sw;

try
{
   sw = new StreamWriter(path);
   sw.WriteLine("sth");

}
finally
{
   sw.Close();
}

Upvotes: 1

Related Questions