Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > File format problem


Reply
 
Thread Tools Display Modes
  #1  
Old 09-08-2000, 04:14 PM
kingesk
Guest
 
Posts: n/a
Question File format problem


'This program should GET a text file, then output only
'the records I want with different field lengths.
'It worked with only 1 record but when I used a file
'with more than one record only the 1st record was formatted
'and the rest were output but not in the correct format (It looked
'like some data was added to the first line that wasn't supposed to be there).
'Also starting in the second line there were fields that were not supposed to be there.

'I have attached a zipped file containg this program and a sample
'text file to input.

'Any sugestions would be greatly appreciated.

Eric King

'PS
'I also noticed some people were attaching their programs to their post
'that did not seem to be zipped. How are they putting both their frm (form file)
'and vbp(project file) in one attachment?



'module level variables
'file name strings
Dim mstrInputFileName As String
Dim mstrOutputFileName As String

'define the input record format
Private Type InputRecord
strType As String * 1
strID As String * 4
strBenefit As String * 4
strApp As String * 3
strLocation As String * 4
strSchedule As String * 2
strDate As String * 8
strSSN As String * 9
strFirst As String * 20
strLast As String * 20
strStreet1 As String * 30
strStreet2 As String * 30
strCity As String * 32
strState As String * 2
strZip As String * 9
strBirthDate As String * 8
strEmpDate As String * 8
strTermDate As String * 8
strRehireDate As String * 8
strSex As String * 1
strKey As String * 1
strStatus As String * 1
strEffDate As String * 8
strSalary As String * 11
strDedCode1 As String * 3
strAmount1 As String * 6
strDedCode2 As String * 3
strAmount2 As String * 6
strDedCode3 As String * 3
strAmount3 As String * 6
strDedCode4 As String * 3
strAmount4 As String * 6
strDedCode5 As String * 3
strAmount5 As String * 6
strDedCode6 As String * 3
strAmount6 As String * 6
strTotalAmount As String * 8
End Type

'define the output record format
Private Type OutputRecord
strFirst As String * 10
strLast As String * 10
strSex As String * 1
strKey As String * 1
strStatus As String * 2
strSSN As String * 9
strBirthDate As String * 8
strEmpDate As String * 8
strSalary As String * 8
strStreet1 As String * 25
strStreet2 As String * 25
strCity As String * 15
strState As String * 2
strZip As String * 9
strDedCode1 As String * 3
strAmount1 As String * 6
strDedCode2 As String * 3
strAmount2 As String * 6
strDedCode3 As String * 3
strAmount3 As String * 6
strDedCode4 As String * 3
strAmount4 As String * 6
strDedCode5 As String * 3
strAmount5 As String * 6
strDedCode6 As String * 3
strAmount6 As String * 6
strDedCode7 As String * 3
strAmount7 As String * 6
strDedCode8 As String * 3
strAmount8 As String * 6
strDedCode9 As String * 3
strAmount9 As String * 6
strTotalAmount As String * 8
strTermDate As String * 8
End Type

Option Explicit

Private Sub cmdExit_Click()
'Exit the application
End
End Sub

Private Sub cmdFormat_Click()
'This method will perform the reformatting of the input file
'and generate an output file of the selected name and path

'define our buffers
Dim udtInputRecord As InputRecord
Dim udtOutputRecord As OutputRecord

'local variables
Dim intNumRecords As Integer
Dim intIndex As Integer

On Error GoTo HandleErrors

'get the input file name
dlgCommon.DialogTitle = "Select Input File"
dlgCommon.InitDir = "a:""
dlgCommon.FileName = vbNullString
dlgCommon.Filter = "*.txt"
dlgCommon.ShowOpen
mstrInputFileName = dlgCommon.FileName

'get output file name
dlgCommon.DialogTitle = "Select Output File"
dlgCommon.InitDir = "a:""
dlgCommon.FileName = vbNullString
dlgCommon.Filter = "*.txt"
dlgCommon.ShowOpen
mstrOutputFileName = dlgCommon.FileName


'clear out the output file
Open mstrOutputFileName For Output As #2
Close #2

'open the input and output files
Open mstrInputFileName For Random As #1 Len = Len(udtInputRecord)
Open mstrOutputFileName For Random As #2 Len = Len(udtOutputRecord)

'perform reformatting function
intNumRecords = LOF(1) / Len(udtInputRecord)
For intIndex = 1 To intNumRecords
Get #1, intIndex, udtInputRecord
udtOutputRecord.strFirst = udtInputRecord.strFirst
udtOutputRecord.strLast = udtInputRecord.strLast
udtOutputRecord.strSex = udtInputRecord.strSex
udtOutputRecord.strKey = udtInputRecord.strKey
udtOutputRecord.strStatus = udtInputRecord.strStatus
udtOutputRecord.strSSN = udtInputRecord.strSSN
udtOutputRecord.strBirthDate = udtInputRecord.strBirthDate
udtOutputRecord.strEmpDate = udtInputRecord.strEmpDate
udtOutputRecord.strSalary = udtInputRecord.strSalary
udtOutputRecord.strStreet1 = udtInputRecord.strStreet1
udtOutputRecord.strStreet2 = udtInputRecord.strStreet2
udtOutputRecord.strCity = udtInputRecord.strCity
udtOutputRecord.strState = udtInputRecord.strState
udtOutputRecord.strZip = udtInputRecord.strZip
udtOutputRecord.strDedCode1 = udtInputRecord.strDedCode1
udtOutputRecord.strAmount1 = udtInputRecord.strAmount1
udtOutputRecord.strDedCode2 = udtInputRecord.strDedCode2
udtOutputRecord.strAmount2 = udtInputRecord.strAmount2
udtOutputRecord.strDedCode3 = udtInputRecord.strDedCode3
udtOutputRecord.strAmount3 = udtInputRecord.strAmount3
udtOutputRecord.strDedCode4 = udtInputRecord.strDedCode4
udtOutputRecord.strAmount4 = udtInputRecord.strAmount4
udtOutputRecord.strDedCode5 = udtInputRecord.strDedCode5
udtOutputRecord.strAmount5 = udtInputRecord.strAmount5
udtOutputRecord.strDedCode6 = udtInputRecord.strDedCode6
udtOutputRecord.strAmount6 = udtInputRecord.strAmount6
udtOutputRecord.strDedCode7 = " "
udtOutputRecord.strAmount7 = " "
udtOutputRecord.strDedCode8 = " "
udtOutputRecord.strAmount8 = " "
udtOutputRecord.strDedCode9 = " "
udtOutputRecord.strAmount9 = " "
udtOutputRecord.strTotalAmount = udtInputRecord.strTotalAmount
udtOutputRecord.strTermDate = udtInputRecord.strTermDate

'write output record to file
Put #2, intIndex, udtOutputRecord
Next

'close the files
Close #1
Close #2

Exit Sub

HandleErrors:
Dim intResponse As Integer

Select Case Err.Number
Case 53, 76, 75 'file or path not found
intResponse = MsgBox("Create a new file?", vbYesNo + vbQuestion, "File not found")
If intResponse = vbYes Then
Resume
Else
Call cmdExit_Click
End If
Case 71 'disk not ready
intResponse = MsgBox("Disk not ready. Retry?", vbRetryCancel + vbQuestion, "Disk Error")
If intResponse = vbRetry Then
Resume 'Try again
Else
Call cmdExit_Click 'Exit project
End If
Case 5
intResponse = MsgBox("Invalid call or procedure (error 5), continue?", vbYesNo + vbQuestion, "Error 5")
If intResponse = vbYes Then
Resume
Else
Call cmdExit_Click
End If
Case Else 'all other errors should cancel execution
Err.Raise Err
End Select
Resume

End Sub
Private Sub Form_Activate()

Left = (Screen.Width - Width) 2
Top = (Screen.Height - Height) 2

'Execute a virus scan
'Shell "C:ericmy-vbshellProject1.exe", vbMaximizedFocus
End Sub




Reply With Quote
  #2  
Old 09-08-2000, 04:52 PM
BillSoo's Avatar
BillSoo BillSoo is offline
Code Meister

Retired Moderator
* Guru *
 
Join Date: Aug 2000
Location: Vancouver, BC, Canada
Posts: 10,441
Default Re: File format problem

Your basic problem is that the input structure doesn't match the data. Each record in the text file you appended is 341 characters long. The structure is only 294 characters.

The simplest fix is to add a buffer at the end of the structure. Something like
strBuffer = string * 47

Also, hardcoding file numbers as #1 and #2 may be OK in a project as simple as this, but you may want to look at the FreeFile function to get the next available file number.

Also, you've hardcoded the DLG path to use "a:"" which causes an error if you don't have anything in drive a:. I prefer app.path instead but you may have a good reason for this. In any case, pressing the cancel button doesn't seem to stop the process.

"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
__________________
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->