5 Replies - 7250 Views - Last Post: 15 August 2011 - 10:47 PM Rate Topic: -----

#1 AN1554  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 130
  • Joined: 03-April 11

Writing text file data to random access file

Posted 04 August 2011 - 10:29 AM

I have a text file which data I must write to a random access file. What I do is open the text file and the random access file; I read one line of the text file and extract the items and store them in a record, then I write the record to the random access file. Then I go to the next line of the text file, extract the items, and so on, until the end of the text file. My code for this is:

    Dim Student As OneStudent
    Dim StudentName As String * 20
    Dim CourseCode As String * 5
    Open TextFile For Input As #1
    Open RandomAccessFile For Random As #2 Len = 3125
    Do While Not EOF(1)
        Input #1, StudentName, CourseCode
        Student.Name = StudentName
        Student.Code = CourseCode
        Put #2, , Student
    Loop
    Close #2
    Close #1


[Note: The length of the random access file is 3125 because there are 125 student records, and each record is 25 (20+5) bytes]

Student is the record variable for the records. I have declared the record type in the General section like this:

    Private Type OneStudent
        Name As String * 20
        Code As String * 5
    End Type


I have used a listbox to display the contents of the random access file to check if it works. But only the first record is there, and after that all is blank. This means that the records haven't been properly written to the random access file. I think the problem is here:

Put #2, , Student


But I don't know what the problem is. I am still trying to figure it out. Any help would be greatly appreciated.

This post has been edited by AN1554: 04 August 2011 - 10:44 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Writing text file data to random access file

#2 Rhymer  Icon User is offline

  • D.I.C Regular

Reputation: 23
  • View blog
  • Posts: 282
  • Joined: 21-April 09

Re: Writing text file data to random access file

Posted 05 August 2011 - 10:38 AM

What you are doing with Len=3125 is putting all student's records into the first record.
What you want is Len=25 for each student record. and the second parameter of put should be the record number. You'll need to do something like x=x+1 each time your read a record from the textfile. then do Put #2, X, Student.

This post has been edited by Rhymer: 05 August 2011 - 11:30 AM

Was This Post Helpful? 0
  • +
  • -

#3 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 572
  • View blog
  • Posts: 2,985
  • Joined: 19-May 09

Re: Writing text file data to random access file

Posted 05 August 2011 - 05:06 PM

Have a look at this as well: http://support.micro....com/kb/150700. So Len refers to length of one record, not length of entire file.
Was This Post Helpful? 0
  • +
  • -

#4 chuckjessup  Icon User is offline

  • D.I.C Regular

Reputation: 33
  • View blog
  • Posts: 380
  • Joined: 26-October 09

Re: Writing text file data to random access file

Posted 05 August 2011 - 09:33 PM

View PostBobRodes, on 05 August 2011 - 05:06 PM, said:

Have a look at this as well: http://support.micro....com/kb/150700. So Len refers to length of one record, not length of entire file.



also there are a ton of stuff with this error... mostly posted by me, the tow other ones who have posted have given excellent answers to your issue.

-Jesse Fender-
Was This Post Helpful? 0
  • +
  • -

#5 AN1554  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 130
  • Joined: 03-April 11

Re: Writing text file data to random access file

Posted 08 August 2011 - 03:35 PM

I missed something out while posting: Put #2, Index + 1, Student. It is that "Index +1" that made the program not work. Because Index remained 0 (as I had not declared any variable to store the new "Index + 1" value each time, e.g. Index = Index + 1, like Rhymer said), and each time a text line would be read, it would be stored only in the first line of the random access file, thus overwriting the last written record. Something like that. And finally, only the last text line was present in the random access file. I've solved it. By the way, the link is very informative. Thanks.
Was This Post Helpful? 1
  • +
  • -

#6 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 572
  • View blog
  • Posts: 2,985
  • Joined: 19-May 09

Re: Writing text file data to random access file

Posted 15 August 2011 - 10:47 PM

Thanks for posting your solution. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1