Read and writing to a binary file
Author |
Message |
timmytheturtle
|
Posted: Sun Sep 19, 2004 7:54 pm Post subject: Read and writing to a binary file |
|
|
What I need help with is im tryin to add names to a binary file using the read and write commands, I can get the name to write but it overwrites the first name in the file, How do I make it write the new name to the end of the list of names? Any help would be greatly appericated, thanks in advance
part of the code:
code: |
proc Output (var sn : int, var names : array 1 .. * of string (15))
open : sn, "Names.bin", write
assert sn > 0
for i : 1 .. upper (names)
write : sn, names (i)
end for
close : sn
end Output
proc OutputNames (var sn : int, var names : array 1 .. * of string (15))
open : sn, "Names.bin", read
for i : 1 .. upper (names)
read : sn, names (i)
put names (i)
end for
delay (1500)
close : sn
end OutputNames
**This is were it writes the new name**
proc AddNames (var sn : int, var names : array 1 .. * of string (15))
open : sn, "Names.bin", write, seek
put "Enter the name to be added to the file"
get names (lower (names))
write : sn, names (lower (names))
close : sn
end AddNames
%MainLine
View.Set ("graphics:600;800,nobuttonbar ")
var names : array 1 .. 5 of string (15) := init ("Chris", "Tom", "Steve", "Kevin", "Tim")
var sn : int
var choice : string (1)
loop
MenU (choice)
if choice = '1' then
Output (sn, names)
elsif choice = '2' then
AddNames (sn, names)
% elsif choice = '3' then
% ReplaceNames (names, sn)
elsif choice = '5' then
OutputNames (sn, names)
elsif choice = 'e' or choice = 'E' then
exit
else
put "not such option"
delay (1000)
cls
end if
end loop
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Delos
![](http://www.members.shaw.ca/rfolz/delos_avatar.gif)
|
Posted: Mon Sep 20, 2004 10:41 am Post subject: (No subject) |
|
|
Hmm...I'm not all to familiar w/ read/write (being a get:/put: person myself) but I think your problem lies in the fact that you're writing directly to the file.
Try to load the entire file into memory at one time, then get your changes, then edit them in memory, and then write them back into your file (clearing everything inside it first).
I'm not sure if this defeats your initial purpose or not though...but it does work. |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Mon Sep 20, 2004 1:14 pm Post subject: (No subject) |
|
|
no delos
the point of random access is just that - access the file randomly
if you want to write to the end of the file, you open it for :write, seek, mod and use seek:* to move to the end of the file. You can add new data there.
somebody confirm if this is right or now, I personally never used random access with turing |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
timmytheturtle
|
Posted: Mon Sep 20, 2004 2:22 pm Post subject: (No subject) |
|
|
thanks for all your help, I hope it works. |
|
|
|
|
![](images/spacer.gif) |
timmytheturtle
|
Posted: Mon Sep 20, 2004 2:40 pm Post subject: (No subject) |
|
|
I just tried it and no luck, it overwrites the first name, but if i put 5(number of names originally in file) it puts the new name with the first name and puts a bunch of characters in fron of the name in the second line.
Edit: I got it to work, I did need to seek to the end of the file |
|
|
|
|
![](images/spacer.gif) |
|
|