Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 deleting record from binary file
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
hey_joe42




PostPosted: Sat Mar 15, 2003 8:53 pm   Post subject: deleting record from binary file

k im trying to have in my program it delete records here is the basics in somewhat pseudocode fashion of what its supposed to do, sorry my pseudocode isn't too good,
get name from user
read the first field(name field) from record, from file
loop
exit when eof(file1)
compare name and namefield and if they are not the same,
write the record to the temp file
end loop

loop
exit when eof(file2)
write file2 to file 1
end if

now here is the code, for some reason this doesn't work that my problem?
code:
%delete
body proc delete
    var fn2 : int
    var deletewho : string (20)
    put "Enter the name of the product to delete"
    get deletewho : *
    open : fn, "inventoryish.bin", read, write, mod, seek
    open : fn2, "temp.bin", write, seek %temporary file to store and move data
    seek : fn, 0
    loop
        exit when eof (fn)
        read : fn, prod
        put deletewho
        put prod.product
        delay(10)
        if prod.product not= deletewho then
put"heartofgold"
delay(20)
            write : fn2, prod   %writes everyfile except one chosen to delete
        end if
    end loop
    seek : fn, 0
    seek : fn2, 0
    loop
        exit when eof (fn2)
        read : fn2, prod
        put prod.product
        write : fn, prod
    end loop
    close : fn
    close : fn2


end delete


if you can't figure out what is wrong plz feel free to run program attached, but enter your on data first, i suggest sample data or instrustions
hit 1 then key in this (mouse,ibm,9.99,2,1,2,111-111,computer,yes : keyboard,ibm,19.99,4,2,4,111-112,computer,no : back)



NP-productmain-v28.t
 Description:

Download
 Filename:  NP-productmain-v28.t
 Filesize:  15.39 KB
 Downloaded:  583 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sun Mar 16, 2003 4:07 pm   Post subject: (No subject)

Quote:

open : fn2, "temp.bin", write, seek %temporary file to store and move

loop
exit when eof (fn2)
read : fn2, prod %<- file was NOT open for READ
put prod.product
write : fn, prod
end loop


thats your problem right there... file was not ment for reading Wink if you also open it for read it works fine. Just one problem... while writing to a binary file, it overwrites it on top, but since it's less in size now, last item remains and you'll have doubles.

the solution? you gotta clear the file first. Ether with blank spaces for the last record, OR delete the whole file using File.Delete (filePathName : string) I suppose that just writing blanks will be easier to do and safer since you not gonna lose data if program suddenly crashes.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: