Computer Science Canada

[tutorial]read and write - extended

Author:  Blade [ Sat Mar 22, 2003 12:38 am ]
Post subject:  [tutorial]read and write - extended

A continuation from a tutorial writen by Dan - http://www.compsci.ca/v2/viewtopic.php?p=29#29

Appending to text files...
Lets say you have a list of something, i'm going to say names... you wouldnt want to use an array to store all the info in the memory, because you could have anywhere from 1 to 10000 names in this datalist. instead you would store the information in a text file

code:

var file,temp:int %declare the files
var name,name1:string
open:file,"name.txt",get %open file to get the names
open:temp,"temp.txt",put % open the temporary file


this next part you have to copy all of the current names to the temporary file for storage, because as soon as you open a file for writing, then it deletes it all

code:

loop
exit when eof(file) %exits when you get to the end of the file
get:file,name %get the name from the first file
put:temp,name %write to the temp file
end loop


now we are going to ask the user for the files he wants to add, and add them to the temporary file, because its already open for putting.

code:

put "Enter the names you would like to add"
put "Exit with q"
loop
get name1
exit when name1 = "q"
put:temp,name1
end loop


now we close the connections and delete the old file, then rename the temporary to what the old one was

code:

close:file
close:temp
File.Delete("names.txt") %deletes the old one
File.Rename("temp.txt", "names.txt")


this is my first tut, i hope it was all of some sort of use to you, tell me if you liked it and i'll continue posting tutorials

MOD Edit: a nice extention of reading/writing to a file tutorial... Smile +15Bits - Tony

Author:  Frazzydee [ Tue Mar 01, 2005 6:35 am ]
Post subject:  Thank you!

I know this is pretty late (only a couple years Wink), but thank you so much for writing that. I've been looking around for a while now for how to do that, but the tutorials/programs I read used some other weird, complicated method. Thanks for making this so easy to understand Smile

Author:  atrain [ Wed Mar 02, 2005 11:05 pm ]
Post subject: 

now if only that worked on macs...

i hate my school... why would they get more macs, after knowing that turing, the main part of most of the computer courses, didnt run properly on them....

they had os 9 macs, so they upgraded to os X and to newer imacs + emacs...


Confused

want to use this im my program, but i get io errors....

Author:  Tony [ Wed Mar 02, 2005 11:51 pm ]
Post subject: 

what kind of an error are you getting?

my mac mini arrives in a week, I'll be able to take a look then if it's a system specific issue.

Author:  atrain [ Thu Mar 03, 2005 11:09 pm ]
Post subject: 

says something about error in the IO stream...

Author:  Frazzydee [ Mon Mar 07, 2005 5:44 pm ]
Post subject: 

atrain wrote:
says something about error in the IO stream...


1. Did you save the file anywhere?
2. Is there a text file (with the specified name) in the same folder?


: