Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
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  
Old August 19th, 2004, 02:22 AM
Lehane Lehane is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: South Africa
Posts: 6 Lehane User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Lehane Send a message via MSN to Lehane Send a message via Yahoo to Lehane
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
Attached Files
File Type: txt test_file_agent_read.txt (198 Bytes, 130 views)
File Type: txt test_file_prod_read.txt (299 Bytes, 125 views)
File Type: txt test_file_write.txt (2 Bytes, 151 views)
File Type: txt This what the result must look like.txt (1.2 KB, 244 views)

Reply With Quote
  #2  
Old August 19th, 2004, 10:31 AM
Mythomep Mythomep is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Zaandam, The Netherlands
Posts: 70 Mythomep User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 37 sec
Reputation Power: 4
Send a message via MSN to Mythomep
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.

Reply With Quote
  #3  
Old August 20th, 2004, 07:41 AM
Lehane Lehane is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: South Africa
Posts: 6 Lehane User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Lehane Send a message via MSN to Lehane Send a message via Yahoo to Lehane
Quote:
Originally Posted by Lehane
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

Thanx a bunch!!! Great help it works now!!!

Reply With Quote
  #4  
Old September 18th, 2004, 12:52 AM
jdarden jdarden is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 8 jdarden User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
'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

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingVisual Basic Programming > Joining text files with a twist


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway