Problem with Input File

vb6 , input file , programming Jiulong, China
  • 12 years ago
    I am just a newbie using VB6 not more than a week. I have to make a project, But I found some problems with input file. The following is part of my project: Dim DataFileName As String Dim DataFile(1 To 100) As String Dim TotalNoOfCand As Integer Dim ContentFile1 As String Dim ContentFile2 As String Dim i, j As Integer Open DataFileName For Input As #2 Do Until EOF(2) Line Input #2, ContentFile2 DataFile(i) = DataFile(i) & ContentFile2 & vbCrLf i = i + 1 Loop TotalNoOfCand = i - 1 For i = 1 To TotalNoOfCand + 1 CandCode(i) = Left(DataFile(i), 6) SchoolCode(i) = Left(DataFile(i), 3) Next i Print ContentFile2 Debug.Print TotalNoOfCand For i = 1 To TotalNoOfCand Debug.Print CandCode(i) Debug.Print SchoolCode(i) Next i The text file(DataFile): 001001BADEBCD 001002BADEBCD 001003CEEBABA 002001BADDAAA 002002ADEBCCC 003002CDCAADC The out put: 003002CDCAADC 001001 001 001002 001 001003 001 002001 002 002002 002 003002 003 003002 003 02 003002 003 There is only "003002CDCAADC" in the ContentFile2. Why?? Can Any One HELP me??
  • 12 years ago
    Oh , The format of Text FIle is wrong correct one: 001001BADEBCD 001002BADEBCD 001003CEEBABA 002001BADDAAA 002002ADEBCCC 003002CDCAADC Also the output 003002CDCAADC 001001 001 001002 001 001003 001 002001 002 002002 002 003002 003
  • 12 years ago
    Hi, my vb6 is a little rusty, and without knowing the format you want to achieve in your output will make it a little harder. What I will do is step through the code to what I think it's doing, and you can have a look to see what it should be doing. Dim DataFileName As String Dim DataFile(1 To 100) As String Dim TotalNoOfCand As Integer Dim ContentFile1 As String Dim ContentFile2 As String Dim i, j As Integer Dim strCompleteFileContents as String ' I am assuming you have already given DataFileName a value ' such as DataFileName = "C:\mydatafile.txt" i = 0 Open DataFileName For Input As #2 Do Until EOF(2) i = i+1 ' now first run, i will equal 1 ' Line input gets a line from the file and stores it into ContentFile2, replacing anything ' that was previous stored in this variable. ' if you want the complete contents of the file, you will need, Line Input #2, ContentFile2 strCompleteFileContents = strCompleteFileContents & ContentFile2 & VbCrLf ' warning, warning, danger will robinson, you haven't set an initial value for i, at first pass, i = nothing 'don't worry, I set it before the loop to equal 0. when the loop starts, we add 1 to i 'bringing initial value of i equal to 1, as it looks like you want your array index to start at ' 1 rather than 0 ' unless there is a some specific reason for this, like DataFile(i) has already got a value, ' shouldn't need to write it like that. I'm assuming DataFile(i) is empty, ' so you could just write it as: DataFile(i) = ContentFile2 ' DataFile(i) = DataFile(i) & ContentFile2 & vbCrLf ' took below out and put it at start of loop. ' i = i + 1 Loop ' you should close the file as below close #2 ' What is TotalNoOfCand? ' because we've moved i to the start of the loop to make the initial datafile(i)equal to 1, ' you may need to change this. ' PS. most people start arrays at 0 index, but starting at 1 is acceptable. TotalNoOfCand = i - 1 msgbox "TotalNoOfCand is equal to: " & TotalNoOfCand ' If I'm not mistaken, TotalNoOfCand should equal number of rows in file - 1, change if not correct. ' you can use a msgbox to test the value. msgboxes are good to use when debugging, ' you can take them out later. 'bed time now.........at least after I do some more work on my cdg karaoke player. I will look at the rest later. For i = 1 To TotalNoOfCand + 1 CandCode(i) = Left(DataFile(i), 6) SchoolCode(i) = Left(DataFile(i), 3) Next i Print ContentFile2 Debug.Print TotalNoOfCand For i = 1 To TotalNoOfCand Debug.Print CandCode(i) Debug.Print SchoolCode(i) Next i
  • 12 years ago
    damm, I hope you can read that. I forgot to use the code formatting.
  • 12 years ago
    Thank you very much. It works.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” - Brian Kernighan