Computing.Net > Forums > Programming > File read line by line and compare

File read line by line and compare

Reply to Message Icon

Original Message
Name: nish_r
Date: June 20, 2006 at 04:05:40 Pacific
Subject: File read line by line and compare
OS: Win XP
CPU/Ram: 1 G
Comment:

hello,

i have 2 text files, one is small (file_1: about 200 lines) and the other is fairly large (file2: 12 k - 20 k lines).

file_1:
tango 12
beta 256
alpha 1700
tango 1200

file_2:
tango 12
tango 125 hello world
beta 256
alpha 1700
Recent gama 5100 hi friend
Test charlie 502 hello dear
.......
....

I need to remove lines in file_2 which contains file_1 entries.
ie. read line by line from file_2, compare with file_1 lines, if match , remove , else append to a new file.
mathces are exact string matches, both are text files.


output_file:
test tango 125 hello world
Recent gama 5100 hi friend
Test charlie 502 hello dear
.......
....

Appreciate your kind assistance to build-up program either in C++ or VB

Thanks,


Report Offensive Message For Removal


Response Number 1
Name: Michael J (by mjdamato)
Date: June 20, 2006 at 12:12:35 Pacific
Reply: (edit)

I can give you VBScript. Try this (not tested):

Dim found
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInputFile2 = objFSO.OpenTextFile ("C:\file_2.txt", True)
Set objOutputFile = objFSO.createtextfile("c:\output_file.txt", True)

Do Until objInputFile2.AtEndOfStream

found = false
strNextLine2 = objInputFile2.Readline

Set objInputFile1 = objFSO.OpenTextFile ("C:\file_1.txt", True)
Do Until objInputFile1.AtEndOfStream
strNextLine1 = objInputFile1.Readline
If (strNextLine2 = strNextLine1) Then
found = true
End If
Loop

objInputFile1.Close
If (found = false) Then
objOutputFile.WriteLine strNextLine2
End If
Loop


objInputFile2.Close
objOutputFile.Close
Set objInputFile1 = nothing
Set objInputFile2 = nothing
Set objOutputFile = nothing

Michael J


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: File read line by line and compare 

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge