Computing.Net > Forums > Programming > vb text file question

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

vb text file question

Reply to Message Icon

Name: heifengxin
Date: October 28, 2003 at 20:25:20 Pacific
OS: win xp pro
CPU/Ram: p4 512mb
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Infinite Recursion
Date: October 28, 2003 at 21:04:06 Pacific
Reply:

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



0

Response Number 2
Name: heifengxin
Date: October 28, 2003 at 23:10:42 Pacific
Reply:

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.


0

Response Number 3
Name: heifengxin
Date: October 28, 2003 at 23:28:25 Pacific
Reply:


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 If

End If

End Sub

Private Sub Command2_Click()
OpenFileDialogue.Hide
End Sub

Private Sub dirDirBox_Change()

'Update the file path to the directory path
filFileBox.Path = dirDirBox.Path

End 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 Sub

Private Sub filFileBox_Click()
Text1.Text = filFileBox.Path + "\" + filFileBox.FileName
End Sub

Private Sub filFileBox_DblClick()
Command1_Click
End Sub


HeiFengXin


0

Response Number 4
Name: Infinite Recursion
Date: October 30, 2003 at 07:03:58 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


MFC question need help circuit related...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: vb text file question

VB text files www.computing.net/answers/programming/vb-text-files/8304.html

vb array using a text file www.computing.net/answers/programming/vb-array-using-a-text-file/5539.html

Adding values in text file via VBS www.computing.net/answers/programming/adding-values-in-text-file-via-vbs/15869.html