Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GET statement error 1

Status
Not open for further replies.

SmokinWrek

IS-IT--Management
Jun 20, 2002
36
0
0
US
Howdy! I hope someone can help me out here. I've got the following code that is supposed to
read in a file of fixed length records, and reverse its order.

Code:
  OutFile = txtOutputFilename.Text
  InFile = txtInputFilename.Text
  TotalCount = fso.GetFile(InFile).Size / RecordSize
  RecNum = TotalCount
  CurrentCount = 0
  InputData = String(RecordSize, Chr(64))
  Open InFile For Random As #1 Len = RecordSize
  Set OutStream = fso.OpenTextFile(OutFile, ForWriting, True)
  For x = RecNum To 1 Step -1
    Get #1, x, InputData
    OutStream.Write InputData
    CurrentCount = CurrentCount + 1
    If ((CurrentCount Mod 1000) = 0) Then
      lblCounter.Caption = CurrentCount & " of " & TotalCount & _
                           " records completed."
      DoEvents
    End If
  Next x

When I try to run this, I get the following error on the Get statement:

Run-time error '59':
Bad record length


I have verified that before the GET statement processes, that x=13068, RecordSize = 400,
and len(InputData) = 400.

I'm probably overlooking something obvious, but could someone please help me out?
 
Try this code, it is untested, but should preform pretty well.

Code:
Dim FSO As Scripting.FileSystemObject
Dim strOriginal As String
Dim strNew As String
Dim arOriginal() As String
Dim arNew() As String
Dim i As Long
Dim j As Long

outfile = txtOutputFilename.Text
infile = txtInputFilename.Text

Set FSO = CreateObject("Scripting.FileSystemObject")
strOriginal = FSO.OpenTextFile(infile, ForReading).ReadAll

arOriginal = Split(strOriginal, vbCrLf)
ReDim arNew(UBound(arOriginal))

j = 0
For i = UBound(arOriginal) To LBound(arOriginal) Step -1
    arNew(j) = arOriginal(i)
    j = j + 1
Next

strNew = Join(arNew, vbCrLf)
Call FSO.CreateTextFile(outfile, True).Write(strNew)
Set FSO = Nothing

The basic steps are...

1) Read all the data from the input file in to a string.
2) Split the string in to an array
3) Loop throught the array backwards putting the data in to a new array
4) Join the new array in to a string
5) Create a new file and store the string in to it.

This procedure will probably be so fast that you don't need a progress indicator.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
chmohan:
I wanted this program to work for any file, any reclen, and I thought I could use the RANDOM access mode if I set
the length of the string with this line:

InputData = String(RecordSize, Chr(64))

Obviously, I was wrong.


gmmastros:
Nice try, but these files won't always have CR/LF delineated records. In fact, they may not always be in
ASCII, they might be in EBCDIC. Also, record size and number of records can be large. I don't think I'd want to store 3 million records at 2,089 bytes each in an array.

So, here's my solution - I just changed my file access to binary, and changed the loop/read statements accordingly:

Code:
  OutFile = txtOutputFilename.Text
  InFile = txtInputFilename.Text
  TotalCount = fso.GetFile(InFile).Size / RecordSize
  RecNum = TotalCount
  CurrentCount = 0
  InputData = String(RecordSize, Chr(64))
  Open InFile For Binary As #1 Len = RecordSize
  Set OutStream = fso.OpenTextFile(OutFile, ForWriting, True)
  For x = RecNum To 2 Step -1
    Get #1, (RecordSize * (x - 1)) + 1, InputData
    OutStream.Write InputData
    CurrentCount = CurrentCount + 1
    If ((CurrentCount Mod 1000) = 0) Then
      lblCounter.Caption = CurrentCount & " of " & TotalCount & " records completed."
      DoEvents
    End If
  Next x
  Get #1, 1, InputData
  OutStream.Write InputData
  CurrentCount = CurrentCount + 1
  lblCounter.Caption = CurrentCount & " of " & TotalCount & " records completed."
  DoEvents
 
SmokinWrek

If your files
1) were less than a couple dozen megabytes
2) and they have a consistent record delimiter
3) and they are ASCII files

then my solution would have worked out really well for you.

I'm glad you were able to spot bad advice when you see it.

From the original post, there appeared to be 13,000 records with 400 bytes per record. That would make your original file be approximately 5.2 megabytes. With a file this size, and assuming the other things I assumed, my process would probably have worked well for you.

Anyway, I'm glad you got things worked out.


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top