Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello all,
I have a function that I need to write in vb that takes two files and compares them (i,e,.)
file #1 (base file)
file #2 (criteria file)the function will take a string from file #2 and look for it in fil #1
if the string is found , it will remove the line in file #1 as well as the three lines proceeding the initial line containing the string
the function will repeate this until file #2 is EOF (end of file.)
any sugguestions?
Thanks in advance
HeiFengXin

Since, you didn't post any code for us to look at, my first suggestion is to attempt this problem first.
My second suggestion is regarding your comment...
"...the function will take a string from file #2 and look for it in fil #1..."Look into using InStr()
IR

I R,
I thank you for your help , actually, I do have code as it pertains to Access Databases, you see, I have never worked directly with files in VB and therefore have no code to share.
I focus primarily on C/C++ and I assume that the structure is quite similiar, given the nature of streams.
As for InStr(), I will look into it,
thanks again.

I R,As you can see I have no file processing functions as of yet, but I did not want anyone to think that I was looking for a free ride, heheh
Actually I quite enjoy programming and am an advocate of the industry, I simply lack the "formal" instruction on the subject.
That said, the only thing I have accomplished thuse far are a few user interface issues.
I am trying out InStr() according to MicroSoft.
o' the joys of online documentation from Master William Gates!
'A File Selection Utility'variables to be used by all functions
Dim sFileName As String
Option Explicit 'General Declarations
Private Sub Command1_Click()
If Text1.Text > "" Then
sFileName = Text1.Text
Dim i
i = MsgBox("USE: " + sFileName + "? ", vbYesNo)
If i = vbNo Then
Exit Sub
End If
If MainForm.txtScrub.Text = "" Then
MainForm.txtScrub.Text = sFileName
Text1.Text = ""
OpenFileDialogue.Hide
Else
MainForm.txtNCL.Text = sFileName
Text1.Text = ""
OpenFileDialogue.Hide
End If
Else
Dim r As Integer
r = MsgBox("Please Select a file to open", vbOKOnly + vbInformation)
If r = vbOK Then
drvDriveBox.SetFocus
Else ' cancel was pressed
End IfEnd If
End Sub
Private Sub Command2_Click()
OpenFileDialogue.Hide
End SubPrivate Sub dirDirBox_Change()
'Update the file path to the directory path
filFileBox.Path = dirDirBox.PathEnd Sub
Private Sub drvDriveBox_Change()
On Error GoTo errorHandler
'Update the directory path to the the drive
dirDirBox.Path = drvDriveBox.Drive
Exit Sub
errorHandler:
Dim message As String
'Check for Device unavailable error
If Err.Number = 68 Then
Dim r As Integer
message = "Drive is not available."
r = MsgBox(message, vbRetryCancel + vbCritical, _
"Please choose another drive")
' Resume?
If r = vbRetry Then
Resume
Else 'cancel was pressed
drvDriveBox.Drive = drvDriveBox.List(1)
Resume Next
End If
Else
Call MsgBox(Err.Description, vbOKOnly + vbExclamation)
Resume Next
End If
End SubPrivate Sub filFileBox_Click()
Text1.Text = filFileBox.Path + "\" + filFileBox.FileName
End SubPrivate Sub filFileBox_DblClick()
Command1_Click
End Sub
HeiFengXin

My suggestion is to look into using file system objects. If these files are basic text files, you can access the various text streams.
TextStream Object Properties and Methods:
Line property: Returns the current line number of the TextStream File.
ReadLine method: Returns the next line from the TextStream file.
SkipLine method: Reads and discards the
next line of a Text Stream.
Basically, what you can do, is have a while
loop that executes for each entry in File 2
(criteria file) and puts that entry into the
string to search for in File 1 (base file).Using the Line property in File 1, you can
keep track of which line you are on while
searching for the string from File 2. IF the
string is found, then send that line number
to an integer variable for reference. Then
have another part of the function (or another
function) create a NEW file (there are methods to do this) based off of File 1 up to three lines prior to the line number of File 1 that you stored for reference, using the SkipLine method, skip 3 or 4 lines in File 1 and write the remainder of the lines to your new file. Delete the original File 1 and save your new file as File 1's name. Ultimately, you will have File 1 that does not include the line with the string you were looking for, nor the three preceeding it.Hope that makes sense. ;)
IR

![]() |
MFC question
|
need help circuit related...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |