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:
  #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, 240 views)
File Type: txt test_file_prod_read.txt (299 Bytes, 241 views)
File Type: txt test_file_write.txt (2 Bytes, 196 views)
File Type: txt This what the result must look like.txt (1.2 KB, 366 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: 6
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





 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

 

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





© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek