Sequential files and updates?

Started by
5 comments, last by Glass_Knife 20 years, 3 months ago
I am taking a c++ class, and our discussion turned to sequential files. A file for a phone book, with a name, address, and phone number, would look like this...

John Smith, 123 Street, 555-1212, Jill Stokes, 345 Another Street, 555-1313, ect...
 
The problem is with updating or deleting records. I have never done this before, but what the instructor said sounded a little odd. He said there was two ways of updating or deleting a record. Read in the whole thing into arrays, change what you need, and write everything back to the file, or have a new output file, and as you are searching for the file you want, put all the records into the output file until the correct file is found. Then change or delete that record, write it to the output( if not deleting ) and then write everything else to the new file. The delete the original file, and rename the output file to the original. This seems like a lot of work to me, but I have never had to make any changes to a text file like this before, so I was not sure if this is how it is done, or is the guy crazy. I am using c++ for the code. Does anyone have any insite into this problem? Glass_Knife I think, therfore I am. I think?

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

Advertisement
quote:Original post by Glass_Knife
I am taking a c++ class, and our discussion turned to sequential files. A file for a phone book, with a name, address, and phone number, would look like this...
John Smith, 123 Street, 555-1212, Jill Stokes, 345 Another Street, 555-1313, ect...  


The problem is with updating or deleting records. I have never done this before, but what the instructor said sounded a little odd. He said there was two ways of updating or deleting a record. Read in the whole thing into arrays, change what you need, and write everything back to the file, or have a new output file, and as you are searching for the file you want, put all the records into the output file until the correct file is found. Then change or delete that record, write it to the output( if not deleting ) and then write everything else to the new file. The delete the original file, and rename the output file to the original. This seems like a lot of work to me, but I have never had to make any changes to a text file like this before, so I was not sure if this is how it is done, or is the guy crazy. I am using c++ for the code. Does anyone have any insite into this problem?

Glass_Knife
I think, therfore I am.
I think?




The discriptions your instructor gave sounds accurate. If someone wanted to change or delete a record you''d read your input file coping the unchanged records to a new file, however when you got to a record that was supposed to be changed you''d write the modified version of the record to the file instead. If the record was suppose to be deleted you just leave it out. Once you''ve finished you can delete the old file and rename the new one. There are probably more effient ways of doing this, but this is probably the simplest.

I hope this helps



Patrick
Does anyone else have any insight into this...
Thanks
Glass_Knife

Glass_Knife
I think, therfore I am.
I think?

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

Yes, what your teacher said are the only 2 ways I know of updating a file in c++ (or java, or perl for that matter). In fact, both his methods are really the same thing in a way if you think about it. Basically you cant modify the middle of a file. You need to completely rewrite the file along with any changes you wanted. If you just wanted to append to the end of the file, there might be a better way to do it, but in general, there is not.

So do you not understand his explanation, or do you just not believe that his explanation is the best way to do it?

Because if its the latter, Im pretty sure that is the best/only way to do what he is asking.


BTW, this sounds like an introductory C++ class, so dont worry about efficiency, unless you teacher tells you to (and he won''t). If you had tons and tons of names to store, then maybe you could have 26 different files, and store people in different files depending on the first letter of their name or something. This would really cut down on the amout of file reading/writing you would have to do... but that is unnecessary for your problem if you ask me.
Just think about it for oh, 12 seconds or so. You are dealing with SEQUENTIAL files. You are dealing with random length data fields. True, they seem to be comma delimited, but they are random length.

How in the world are you going to ''guess'' exactly where on disk record 153 is? How can you edit it even if you did? Example: Mary Smith from 1 Main Street gets married, and then becomes Mary Abawabinichakery from 134 Thringingdown Avenue. Where do you cram the extra characters?

This is of course one reason that sequential files are almost never used for dynamic, random access storage
Thanks to everyone who has shed light onto this. I shall slap myself on the wrist. I guess because he was showing examples in QBASIC I assumed that he didn''t know what he was talking about. I won''t make that mistake again. Thanks again.

Glass_Knife
I think, therfore I am.
I think?

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

quote:Original post by Glass_Knife
I guess because he was showing examples in QBASIC I assumed that he didn''t know what he was talking about.

I like that attitude :-) Might even make it into my signature


---------------------------
I may be getting older, but I refuse to grow up
I may be getting older, but I refuse to grow up

This topic is closed to new replies.

Advertisement