• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

File I/O question

I want to be able to scan through a file, find a peice of data, then once ive found this data i want to write new data over top of it, Anyone know how to do this? I try to use the Open ... method but i cant open a file and read and write to it useing this method.

THanks!
0
Czeh
Asked:
Czeh
  • 2
  • 2
  • 2
1 Solution
 
FoyalCommented:
Dim v as integer
Dim i as integer
v = FreeFile

i = 0

Open FileName for Random Read Write as #v Len = Len(Record)

Do While Not EOF(v)
    i = i + 1
    Get #v, i, Record
    If Record <is the right one> Then
        Put #v, i, NewRecord
        Exit Do
    End If
Loop

Close #v
0
 
CzehAuthor Commented:
It first says invalid procedure call on the "Open" line, so i tryed taking off the Len=Len(Record) that works then it says "Variable uses an automation type not supported by VB" in the get statement. Hmmm, btw this is what my file looks like:

"\\w0708204\pr4s_c"
""
"fix32\alm"
"dfgeh"
"cbcb"
"000202.ALM"
""
"pr4s"
"\\w0708206\pr6s_c"
""
"fix32\alm"
"sdfh"
"cbcb"
"000202.alm"
""
"ul1s"
"\\w0708208\ul1s_c"
""
"fix32\alm"
"addc"
"abc"
"000202.alm"
""
0
 
kfrickCommented:
Foyal was on the right track......
First you must define a template for your data file. I can't tell from your data exactly how it's laid out.
First, define a data Type for you file elements, and declare one (here's one for meat products):

Type ProductRecord
    PLU AS Integer
    PID As String * 9
    UPC_ID As Long
    Description1 As String * 32
    Description2 As String * 32
    Description3 As String * 32
End Type
Global prCurrentProduct As ProductRecord

Your elements can be anything you wish. A lot of people define everything with strings. You must declare the length of each string.

You then define data for each element:

With prCurrentProduct
  .PLU = 123
  .PID = "123456789"
  .UPC_ID = 22345
  .Description1 = "TEST LINE 1"
  .Description2 = "TEST LINE 2"
  .Description3 = "TEST LINE 3"
End With

Here's where Foyal's code, or at least a little of it comes in. This opens  afile and stores this recor as the "first" product:

Open "TESTFILE" for Random as #1 Len = Len(prCurrentProduct)
Put #1, prCurrentProduct
Close #1

You can read it back using:

Open "TESTFILE" for Random as #1 Len = Len(prCurrentProduct)
Get #1, prCurrentProduct
Close #1

If you have more than 1 "product" you just change the "Put #" to recall or write the record for that product. That's to loop in Foyal's code...to search for the correct record.

It al starts with defining your data structure....good luck!

-kf






0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
FoyalCommented:
Thanks KFrick... I had seen Czeh's response to my answer, but I didn't have time to clarify my answer at the time. The user defined type is the secret to the whole thing.
See 'ya,
Foyal
0
 
CzehAuthor Commented:
This is good, i need a little bit more info, currently my lines of text in my file change and have varying lengths, is their any way to read only one line, or 1 'set' of the file without haveing to know the length?


BTw thanks for all the help
0
 
kfrickCommented:
Set the Type definition to the Longest Length required for each variable....shorter lengths will be stored with enough space behind them to make everything "line up" when you attempt to retrieve the data.
Is this a file you are createing, or just trying to read?

Alternately, you can open the file for INPUT and read it in using the LINE INPUT # statement. You could even test for the required data. This method is Much Slower, but may work for you:

Open "TestFile" For Input As #1
Open "NewFile" For Output AS #1
While Not EOF(1)
  Line Input #1, Data$
  Debug.Print Data$
  If Data$ = {Your test here} Then
    Data$ = NewData$
  End If
  Print #2, Data$
Wend
Close #1
Close #2

Then rename your old file as a backup, and rename your new file as the old file.

This is not so easy yet, is it!!!???

-kf
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now