|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Joining text files with a twist
Hi all,
I need to -Read the two files line by line into a variable. -the first file contains agent codes, the second file contains the products. -I need to join each agent code to all the products of the second text file...till there is no more records -and save the output into a result text file. Here's my code: Dim InputFileNum1 As Integer Dim InputFileNum2 As Integer Dim OutputFileNum As Integer Dim Buffer1 As String Dim Buffer2 As String Private Sub Command1_Click() Dim line_f1 As String Dim line_f2 As String Dim final_line As String ' Get the input file handle and open for input-only InputFileNum1 = FreeFile InputFileNum2 = FreeFile Open "C:\Documents and Settings\Administrator\Desktop\New Folder\my test\test_file_agent_read.txt" For Input As InputFileNum1 ** Open "C:\Documents and Settings\Administrator\Desktop\New Folder\my test\test_file_prod_read.txt" For Input As InputFileNum2 ' Get the output file handle and open/create the file OutputFileNum = FreeFile Open "C:\Documents and Settings\Administrator\Desktop\New Folder\my test\test_file_write.txt" For Output As OutputFileNum ' Read each line in the input file one line at a time While Not EOF(InputFileNum1) ' Reads one line of text and assigns it to the buffer and then to string Line Input #InputFileNum1, Buffer1 line_f1 = Buffer1 While Not EOF(InputFileNum2) Line Input #InputFileNum2, Buffer2 line_f2 = Buffer2 Wend final_line = line_f1 & Space(5) & line_f2 Print #OutputFileNum, Buffer1, Buffer2 Wend Close #OutputFileNum Close #InputFileNum1 Close #InputFileNum2 End Sub (NOTE I used ** to display where debugger gives error) ** It gives me a Runtime error '55': File already open Can you please help me...What am i doing wrong? Is there another way? ==Please find attached the sample text files. Thanx in advance! L |
|
#2
|
|||
|
|||
|
Hi,
Error you get is because the InputFileNum is zero when started, and you cannot open a different file with the same filehandle. Code:
Inputfilenum1 = Freefile Open "C:\File.txt" for input as InputFileNum1 Inputfilenum2 = FreeFile Open "C:\File2.txt" for input as InputFileNum2 Don't forget to close the files when you are done with them. Otherwise you might get unexpected results. Grtz.© M. |
|
#3
|
|||
|
|||
|
Quote:
Thanx a bunch!!! Great help it works now!!! |
|
#4
|
|||
|
|||
|
'FreeFile does not increase until you actually open a file
Do this: InputFileNum1 = FreeFile Open "c:\Documents and Settings\Administrator\Desktop\test_file_agent_rea d.txt" For Input As InputFileNum1 InputFileNum2 = FreeFile Open "c:\Documents and Settings\Administrator\Desktop\test_file_prod_read .txt" For Input As InputFileNum2 Not This: InputFileNum1 = FreeFile InputFileNum2 = FreeFile Open "c:\Documents and Settings\Administrator\Desktop\test_file_agent_rea d.txt" For Input As InputFileNum1 Open "c:\Documents and Settings\Administrator\Desktop\test_file_prod_read .txt" For Input As InputFileNum2 |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Joining text files with a twist |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|