5 Replies - 960 Views - Last Post: 11 June 2014 - 08:26 AM Rate Topic: -----

#1 alexvg  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 88
  • Joined: 05-June 14

Writing to Sequential File Problem

Posted 11 June 2014 - 07:46 AM

Hi, I am having some troubles and I am unsure why.

I am trying to write student records to sequential file, but for some odd reason once I save one record, and try to save a new one, the earlier one is replaced by the new one, so I only have 1 name and one grade in the sequential file at all times. please help.

 Dim Record As String

        If txtName.Text = "" Or Not IsNumeric(txtAssignment1.Text) Or Not IsNumeric(txtAssignment2.Text) Or Not IsNumeric(txtAssignment3.Text) Or Not IsNumeric(txtAssignment4.Text) Or Not IsNumeric(txtMidterm.Text) Or Not IsNumeric(txtFinal.Text) Then
            MessageBox.Show("invalid or missing data in Text Boxes")
        Else

            Try
                Dim objWriter As StreamWriter
                objWriter = New StreamWriter("C:\Users\Alex\Desktop\Visual Basic\Assignments\Assignment 3\StudentMarks.txt")

                Record = txtName.Text + vbTab + txtTotal.Text


                objWriter.WriteLine(Record)


                MessageBox.Show("File has been updated.", "Saved File")

                objWriter.Close()

            Catch ex As Exception
                MessageBox.Show("Unable to write to file" & StudentMarks, "File I/O Error")
            End Try

        End If 


Is This A Good Question/Topic? 0
  • +

Replies To: Writing to Sequential File Problem

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Writing to Sequential File Problem

Posted 11 June 2014 - 07:57 AM

The StreamWriter class provides a constructor with extra boolean parameter, which when set to true, means append and false(default) means overwrite.
objWriter = New StreamWriter("path", True)


Was This Post Helpful? 1
  • +
  • -

#3 alexvg  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 88
  • Joined: 05-June 14

Re: Writing to Sequential File Problem

Posted 11 June 2014 - 08:04 AM

Hi Smohd,

Would this do what I want?

 Dim strStudentMarks As String

        strStudentMarks = ("C:\Users\Alex\Desktop\Visual Basic\Assignments\Assignment 3\StudentMarks.txt")

        Try

            FileOpen(1, strStudentMarks, OpenMode.Append)

            Print(1, txtName.Text + vbTab + txtTotal.Text + vbCrLf)

            Print(1, "" & vbCrLf)

            MessageBox.Show("File Updated, New Data Stored!", "Saved!")

            FileClose(1)
        Catch ex As Exception
            MessageBox.Show("Unable to write to file " & strStudentMarks, "File I/O Error", _
 MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try



Was This Post Helpful? 0
  • +
  • -

#4 Sheepings  Icon User is offline

  • D.I.C Addict

Reputation: 73
  • View blog
  • Posts: 571
  • Joined: 05-December 13

Re: Writing to Sequential File Problem

Posted 11 June 2014 - 08:11 AM

I don't know, would it? One would assume though that you are writing a file and you want to keep the writing to the same file over and over without wiping out the contents placed previously? Look what smohd said, as it should do what you need in that case. Append your code

objWriter = New StreamWriter("C:\Users\Alex\Desktop\Visual Basic\Assignments\Assignment 3\StudentMarks.txt", True)'Or False depending on how you want it to function.

Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Writing to Sequential File Problem

Posted 11 June 2014 - 08:13 AM

Why did you change the whole code you had? All you have to do is adding True as the second parameter to the StreamWriter constructor.
Was This Post Helpful? 1
  • +
  • -

#6 Sheepings  Icon User is offline

  • D.I.C Addict

Reputation: 73
  • View blog
  • Posts: 571
  • Joined: 05-December 13

Re: Writing to Sequential File Problem

Posted 11 June 2014 - 08:26 AM

Alexvg, use your original code you posted and make the alteration suggested in post 2 to that code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1