Solved

read data line by line from a textfile

Posted on 2007-03-21
6
163 Views
Last Modified: 2010-04-30
Hi: Can any one please tell me how to i read data line by line from a textfile and then write into another textfile?

Thanks.
0
Comment
Question by:mustish1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 27

Expert Comment

by:VBRocks
ID: 18765986
Are you using VB6 or VB.NET?

VB6:  Add a reference to the Microsoft Scripting Runtime Library (scrrun.dll) Project / References.  

Then, code like this:
Dim fso as new FileSystemObject
Dim ts as TextStream
Dim inLine as string

'To Read:
set ts = fso.OpenTextFile("c:\temp\myfile.txt", ForReading)
Do
     inLine = ts.ReadLine
     'Process with your line.
Loop Until ts.AtEndOfStream = True
ts.close

'To Write:
set ts = fso.OpenTextFile("c:\temp\myfile.txt", ForWriting)
ts.WriteLine "Cool!"
ts.Close

Do you need it in VB.NET?

0
 

Author Comment

by:mustish1
ID: 18766001
Thanks only in VB6
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 18766028
Here's VB.NET:
Dim inLine as String

'To read
Dim sr As New System.IO.StreamReader("c:\temp\myfile.txt")
Do
     inLine = sr.ReadLine
Loop until sr.EndOfStream = True
sr.Close

'To write
Dim sw as New System.IO.StreamWriter("c:\temp\myfile.txt")
sw.WriteLine("Cool!")
sw.Close()
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mustish1
ID: 18766035
Thanks can you please tell me how to i read one line from firstfile.txt and write into second file.txt until firstfile.txt is at end.

Dim fso as new FileSystemObject
Dim ts as TextStream
Dim inLine as string

'To Read:
set ts = fso.OpenTextFile("c:\temp\firstfile.txt", ForReading)
Do
     inLine = ts.ReadLine
     'Process with your line.
Loop Until ts.AtEndOfStream = True
ts.close

'To Write:
set ts = fso.OpenTextFile("c:\temp\secondfile.txt", ForWriting)
ts.WriteLine "Cool!"
ts.Close
0
 
LVL 27

Accepted Solution

by:
VBRocks earned 500 total points
ID: 18766063
Sure, it's the same, just create 2 variables:

Dim fso as new FileSystemObject
Dim tsRead as TextStream
Dim tsWrite as TextStream
Dim inLine as String

set tsRead = fso.OpenTextFile("c:\temp\firstfile.txt", ForReading)
set tsWrite = fso.OpenTextFile("c:\temp\secondfile.txt", ForWriting)

Do
     'Read the line from the firstfile
     inLine = tsRead.ReadLine

     'Write the line to the secondfile
     tsWrite.WriteLine inLine

Loop Until tsRead.AtEndOfStream = True

tsRead.close
tsWrite.close

Let me know if you have any other questions!
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 18766099
Don't forget to add the Project Reference, or you won't be able to use the FileSystemObject:

Add a reference to the Microsoft Scripting Runtime Library (scrrun.dll) Project / References.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question