Jump to content
  • Advertisement
Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

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 = ""

            While Me.m_InputStream.Peek <> -1
                strStringRead = Me.m_InputStream.ReadLine()

                '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
                    'add and read the next part
                    WriteData(strStringRead, iDataWritten)
                    '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)
            If ((iDataWritten + strStringToWrite.Length + 1) >= Me.m_kMaxFileSize) Then
                iDataWritten = 0

                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)

            '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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!