Results 1 to 13 of 13

Thread: appending to a file

  1. #1

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91

    appending to a file

    I have a list of numbers from a file that looks like this:

    "001","Potatoes, red",1.15
    "007","Potatoes, Baking",.98
    "010","Peppers, Red",1.1
    "011","Peppers, Green",.75
    "015","Peppers, Yellow",1.4
    "050","Chicken Soup",.9
    "055","Beef Soup",.98
    "060","Rice, Converted",2.1
    "061","Rice, Instant",1.5
    "084","pretzels",2.14
    "096","napkins",1.45
    "100","Pencil, #2",.29
    "101","Pen, Blue",.33
    "102","Pen, Red",.33
    "110","Clips, 110ct",.4
    "120","Scotch Tape",.78
    "130","Packing Tape",.49
    "135","Masking Tape",.27
    "200","T-Bone",3.78
    "201","Ground Round",2.95
    "210","Chicken Tenders",1.98
    "340","Salmon",4.55
    "409","Soap",1.47
    "428","Cheddar Cheese",2.15
    "466","Milk, 1 Gal",1.18
    "467","Milk, 1 Pt",.87
    "501","Yogurt, Raspberry",1.78
    "502","Yougurt, Bluebarry",1.78
    "503","Yogurt, Plain",1.28
    "601","Cola, 2 ltr",1.29
    "602","Cola, diet 2 ltr",1.29
    "603","Cola, 6 pack",1.85
    "701","Gum, Spearmint",.95
    "711","Kisses",1.48
    "712","Almond Kisses",2.15
    "812","House, 2000 sq ft",150000
    "904","Car, Sports",27500
    "905","Car, Sedan",22897
    "910","Bug",15679
    "990","prod 990",9.9
    "999","cumquats",100

    I have inported them into an array I like this:
    Code:
        ProdFileName = CDL1.FileName            '**  Remember product file name
        Open ProdFileName For Input As #6       '**  Open the product file
        I = 1                                   '**  Start with first product
        Do Until EOF(6)                         '**  Until the end of the file
            Input #6, UPC(I), ProdDesc(I), ProdPrice(I) '**  Read I-th product
            I = I + 1                           '**  Anticipate next product
        Loop
        
        NumProducts = I - 1                     '**  Remember number of products read
        Close #6
    Now, if I call up a certain UPC line of code, say like this:

    NewUPCTrans = UPC(I)

    How can I append more information into the ProdFileName so that the file will look like this:

    "001","Potatoes, red",1.15,2,1.3
    "007","Potatoes, Baking",.98,7,6.5
    "010","Peppers, Red",1.1
    "011","Peppers, Green",.75
    "015","Peppers, Yellow",1.4
    "050","Chicken Soup",.9,3,2.7
    "055","Beef Soup",.98
    "060","Rice, Converted",2.1
    and so on....

    basically, I want to make the 2 new bits of information new veribles in the array that I can recall to.

    Can someone tell me how to do this??

    Thanks

    Brian
    fujiyama17 (representing KSU)
    -=-=-==-=-=
    "There is something funny about life. If you never settle for anything but the very best, you will very often recieve it."
    -Margaret M. Somet

  2. #2
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    You want to add items to an array or to a text file? I didn't quite understand which.


    To add items to an array you do as so.

    Redim Preserve Myarray(Ubound(Myarray)+1)
    MyArray(Ubound(Myarray)) = "The new text"

    To add to the end of a text file just use Open for append.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  3. #3
    Megatron
    Guest
    Code:
    Open "File" For Append As #1
    Write #1, NewUPCTrans NewProcDesc,NewProdPrice
    Close #1

  4. #4

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91
    ANYONE!?!?

    All I want to do is append to the file, without changing the information that is already there.

    HELP!!

    brian

    thanks. i will give anyone free reign to my body if they can answer my questions completly.
    fujiyama17 (representing KSU)
    -=-=-==-=-=
    "There is something funny about life. If you never settle for anything but the very best, you will very often recieve it."
    -Margaret M. Somet

  5. #5
    Megatron
    Guest
    Did you try the code I gave you?

  6. #6
    Fanatic Member joltremari's Avatar
    Join Date
    Sep 2000
    Location
    Mississippi
    Posts
    674
    VB Code:
    1. 'If you are changing information use:
    2. Open ProdFileName For Output As #6      
    3.  
    4. 'and overwrite the File
    "I have not failed. I've just found 10,000 ways that won't work."
    'Thomas Edison'

    "If we knew what it was we were doing it wouldn't be called research, would it?"
    'Albert Einstein'

    VB6

  7. #7
    Megatron
    Guest
    Keep in mind, though, that opening it in Output mode will cause windows to start writing at the beginning of the file (thus the contents will be erased) whereas using Append will instruct windows to start writing at the end of the file (thus the contents are not overwritten).

  8. #8

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91
    the UPC number is looked up when a UPC Code is put in. What I want to do is append a number to the file.... specifically, a counter, so that when I recall that UPC line of code again, I can output the mount of times it was used... so for example I have this as my dat file.

    "210","Chicken Tenders",1.98
    "340","Salmon",4.55
    "409","Soap",1.47
    "428","Cheddar Cheese",2.15
    "466","Milk, 1 Gal",1.18
    "467","Milk, 1 Pt",.87
    "501","Yogurt, Raspberry",1.78
    "502","Yougurt, Bluebarry",1.78
    "503","Yogurt, Plain",1.28
    "601","Cola, 2 ltr",1.29
    "602","Cola, diet 2 ltr",1.29
    "603","Cola, 6 pack",1.85
    "701","Gum, Spearmint",.95

    now, you put it "502" and it diplays "Yougurt, Bluebarry" and gives you the price 1.78 . Can I put a 1 right next to that particular line of info, I suppose using the Append file. so that it looks like this:

    "210","Chicken Tenders",1.98
    "340","Salmon",4.55
    "409","Soap",1.47
    "428","Cheddar Cheese",2.15
    "466","Milk, 1 Gal",1.18
    "467","Milk, 1 Pt",.87
    "501","Yogurt, Raspberry",1.78
    "502","Yougurt, Bluebarry",1.78,1 <-------
    "503","Yogurt, Plain",1.28
    "601","Cola, 2 ltr",1.29
    "602","Cola, diet 2 ltr",1.29
    "603","Cola, 6 pack",1.85
    "701","Gum, Spearmint",.95

    How do I do this?

    Brian
    fujiyama17 (representing KSU)
    -=-=-==-=-=
    "There is something funny about life. If you never settle for anything but the very best, you will very often recieve it."
    -Margaret M. Somet

  9. #9

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91

    would this work??

    what if i read in varibles that weren't there to begin with. Would that work? Like this:

    Code:
        ProdFileName = CDL1.FileName            '**  Remember product file name
        Open ProdFileName For Input As #6       '**  Open the product file
        I = 1                                   '**  Start with first product
        Do Until EOF(6)                         '**  Until the end of the file
            Input #6, UPC(I), ProdDesc(I), ProdPrice(I),ProdCounter(I),ProdTotalPrice(I) '**  Read I-th product
            I = I + 1                           '**  Anticipate next product
        Loop
        
        NumProducts = I - 1                     '**  Remember number of products read
        Close #6
    ProdCounter(I) & ProdTotalPrice(I) don't exist b/c the code looks like this:

    "210","Chicken Tenders",1.98
    "340","Salmon",4.55
    "409","Soap",1.47
    "428","Cheddar Cheese",2.15
    "466","Milk, 1 Gal",1.18
    "467","Milk, 1 Pt",.87
    "501","Yogurt, Raspberry",1.78
    "502","Yougurt, Bluebarry",1.78
    "503","Yogurt, Plain",1.28
    "601","Cola, 2 ltr",1.29
    "602","Cola, diet 2 ltr",1.29
    "603","Cola, 6 pack",1.85
    "701","Gum, Spearmint",.95

    but, this would allow me to make changes to individual lines. Is this the right way of doing it??
    fujiyama17 (representing KSU)
    -=-=-==-=-=
    "There is something funny about life. If you never settle for anything but the very best, you will very often recieve it."
    -Margaret M. Somet

  10. #10
    Megatron
    Guest
    Well in that case, you are modifying the contents, so you need to use Output. But I guess that isn't a problem, since you got all of your info stored in the array.
    VB Code:
    1. MyNewData = 1
    2.  
    3. Open "File" For Output As #1
    4.     For i = 0 to Ubound(UPC)
    5.         If UPC(i) = "502" Then
    6.             Write #1,UPC(i),ProdDesc(i),ProdPrice(i),MyNewData
    7.         Else
    8.             Write #1,UPC(i),ProcDesc(i),ProdPrice(i)
    9.         End If
    10.     Next i
    11. Close #1

  11. #11
    Fanatic Member joltremari's Avatar
    Join Date
    Sep 2000
    Location
    Mississippi
    Posts
    674
    If you append you will end up with this:

    "210","Chicken Tenders",1.98
    "340","Salmon",4.55
    "409","Soap",1.47
    "428","Cheddar Cheese",2.15
    "466","Milk, 1 Gal",1.18
    "467","Milk, 1 Pt",.87
    "501","Yogurt, Raspberry",1.78
    "502","Yougurt, Bluebarry",1.78,1 <--------
    "503","Yogurt, Plain",1.28
    "601","Cola, 2 ltr",1.29
    "602","Cola, diet 2 ltr",1.29
    "603","Cola, 6 pack",1.85
    "701","Gum, Spearmint",.95
    "502","Yougurt, Bluebarry",1.78,1 <--------

    But if all of your info is stored in an array then if you use Output and it will overwrite everything. So anything you don't change stays the same and anything you do change gets changed.
    "I have not failed. I've just found 10,000 ways that won't work."
    'Thomas Edison'

    "If we knew what it was we were doing it wouldn't be called research, would it?"
    'Albert Einstein'

    VB6

  12. #12
    Fanatic Member joltremari's Avatar
    Join Date
    Sep 2000
    Location
    Mississippi
    Posts
    674
    Originally posted by Megatron
    VB Code:
    1. MyNewData = 1
    2.  
    3. Open "File" For Output As #1
    4.     For i = 0 to Ubound(UPC)
    5.         If UPC(i) = "502" Then
    6.             Write #1,UPC(i),ProdDesc(i),ProdPrice(i),MyNewData
    7.         Else
    8.             Write #1,UPC(i),ProcDesc(i),ProdPrice(i)
    9.         End If
    10.     Next i
    11. Close #1
    Salute
    "I have not failed. I've just found 10,000 ways that won't work."
    'Thomas Edison'

    "If we knew what it was we were doing it wouldn't be called research, would it?"
    'Albert Einstein'

    VB6

  13. #13
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,526
    If your file is a text file, then the easiest way to update it is to read it all into memory (array) and then write it ALL back out to disk. It is possible to acces it non-sequentially, but the code you would need to do that will be much more complex than you need, unless you have a very large file. Or, you could set up your file as a database (random access) and then update the fields individually without any trouble.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width