Anyone know how to set a maximum text file size?

-Strict-

Active member
Joined
Jan 18, 2003
Messages
32
For instance, say you got a program that keeps a log of the activities it performs. Does anyone have any idea on how to set a max size you would want that file to be? like say 1mb.

I was thinking maybe there is some way to load the pre-existing contents into an array of somekind and seeing how big it is, and then adding to the array throughout the runtime of the program then write it out when finished.. That way you could even reverse the order of the log, putting the newest entries at the top for easy viewing.

the only problem with that is I want to flush the buffer and write to the file after every logged action. That way if something goes wrong and crashes at least there is something in the log file.


Any ideas?
 
You could count the characters. I just did a test with a text file and found that one character is one byte and a new line is 2 bytes.
 
If your text file is in unicode, there will be two bytes per character. The easiest method of checking a files size is to use the FileInfo class and check its Length property.
 
Yes it does, actually I found a nice little solution to this, heres the code:

Code:
 limit the files to 250K 
Dim TR As System.IO.TextReader
Dim sTemp As String = ""
Dim iTot As Integer = 0
Dim sTempRemoved As String = ""
TR = New System.IO.StreamReader(m_AppName)
sTemp = TR.ReadToEnd()
TR.Close()
iTot = sTemp.Length - 250000
If iTot < 0 Then
   iTot = 0
End If
sTempRemoved = sTemp.Remove(0, iTot)
TWR = New System.IO.StreamWriter(m_AppName, False)
TWR.Write(sTempRemoved)
TWR.Close()

or in c# how I originally got it

Code:
//limit the files to 250K 
            System.IO.TextReader TR; 
            string sTemp = ""; 
            int iTot = 0; 
            string sTempRemoved = ""; 
            TR = new System.IO.StreamReader(m_AppName); 
            sTemp = TR.ReadToEnd(); 
            TR.Close(); 
            iTot = sTemp.Length - 250000; 
            if (iTot < 0){iTot = 0;} 
            sTempRemoved = sTemp.Remove(0, iTot); 
            TWR = new System.IO.StreamWriter(m_AppName,false); 
            TWR.Write(sTempRemoved); 
            TWR.Close();
 
Back
Top