Public Group

vb.net 2003 writting to a file with a size limit

This topic is 4531 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm trying to chunck up files that are larger then 2048 bytes into files less then 2048 bytes, but I am having a problem doing this. Is there a way with System.IO.StreamWriter to keep track of the data writtin? I tried to do it manually, but it only works about 60% of the time. Here is what I have now if anyone can find a flaw in my function.
'This is the input file that we are reading from (The book that we are getting all the data).s
Private m_InputStream As System.IO.StreamReader = Nothing

'This is the Currently open file that we are writing to.
Private m_OutputStream As System.IO.StreamWriter = Nothing

Private Sub m_Create_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles m_Create.Click
'This is what we're reading from the file
Dim strStringRead As String = ""

Dim iDataWritten As Integer = 0

'This is what we're going to be writting to the file
'Dim strStringToWrite As String = ""

Try
While Me.m_InputStream.Peek <> -1

'If the total size of the string is less then 2048 bytes (2 KB) then add and read the next line,
'other wise we must stop and write.
If (strStringRead.Length < Me.m_kMaxFileSize) Then
Else
'the string needs to be chuncked
End If

End While
Catch ex As Exception

End Try

End Sub

'This function will create the File and write the table of contents
Private Sub WriteData(ByVal strStringToWrite As String, ByRef iDataWritten As Integer)
Try
If ((iDataWritten + strStringToWrite.Length + 1) >= Me.m_kMaxFileSize) Then
iDataWritten = 0

Me.m_OutputStream.Close()
Me.m_OutputStream = Nothing
End If

If Me.m_OutputStream Is Nothing Then
Dim strFilename As String = New String(Me.m_OutputFile.Text + "\" + "page" + CType(Me.m_iPageNumber, String) + ".txt")
Me.m_OutputStream = New System.IO.StreamWriter(strFilename, False, System.Text.Encoding.ASCII)

Me.m_iPageNumber += 1
End If

'Dim strFilename As String = New String(Me.m_OutputFile.Text + "\" + "page" + CType(Me.m_iPageNumber, String) + ".txt")
'Dim OutputStream As System.IO.StreamWriter = New System.IO.StreamWriter(strFilename, False, System.Text.Encoding.ASCII)

Me.m_OutputStream.WriteLine(strStringToWrite)
'add one for newline characters
iDataWritten += strStringToWrite.Length + 1

Catch ex As Exception

End Try
End Sub


EDIT: Private Const m_kMaxFileSize As Integer = 2048 that should've been in there too. thanks in advance, ~guyaton

• 10
• 17
• 9
• 14
• 41