Computer Science Canada opening files |
Author: | awesomej01 [ Sun May 30, 2004 4:22 pm ] |
Post subject: | opening files |
I'm trying to write an algorithm that can take a file for eg. abc and open it then output it as cba. I tried putting it into a for decreasing loop but that didn't work, then I tried using a normal loop but that didn't work either. If anyone don't mind I hope they can tell me what is wrong with my code and help me fix it. thanks. var filestream : int var words : string var CountingThingy2, CountingThingy : int := 0 %Count how many characters are in the file open : filestream, "temp.txt", get loop exit when eof (filestream) get : filestream, words : 1 CountingThingy := CountingThingy + 1 end loop close : filestream put CountingThingy %Method 1 open : filestream, "temp.txt", get loop get : filestream, words : 1 CountingThingy2 := CountingThingy2 + 1 if CountingThingy2 = CountingThingy then put words .. CountingThingy2 := 0 CountingThingy := CountingThingy - 1 end if exit when CountingThingy = 0 end loop close : filestream %Method 2 open : filestream, "temp.txt", get for decreasing i : CountingThingy .. 1 get : filestream, words : 1 put words .. end for close : filestream Neither of these methods work. |
Author: | Delos [ Sun May 30, 2004 4:33 pm ] |
Post subject: | |
Wow...this code is...wow Ok. First things first. 1) use [ code][/code] tags. 2) you may want to have attatched the 'temp.txt' file of which you speak. We could always create one for ourselves...but that could be tedious... - you are getting one letter at a time...why! That makes your life difficult. Your initial little piece there, the one that counts the number of letters in the file, also counts the eof...so that's no good. Just get the entire line (using :*) and then length() it. - once you have the required file contents in a string, reversing it is no problem...just use a (decresing) for loop...nothing difficult. - if you need to write this new string to a file, put: will do fine. Try that, then repost if you're still on a limb...or two... |
Author: | naoki [ Sun May 30, 2004 9:43 pm ] | ||
Post subject: | |||
so you want "abc" to become "cba"? like delos stated, your method is very inefficient. getting from a text file one letter at a time is very time consuming and kinda slow. why don't you get a whole line ( get: file, word : *) and then go through a for loop (again, like delos stated) from 1 .. length (word). then make a temporary string holder (tempword) and add one letter at a time from word to tempword
[/code] |
Author: | McKenzie [ Tue Jun 01, 2004 7:22 am ] | ||
Post subject: | |||
<seems to be some confusion in the replys> Quote: I'm trying to write an algorithm that can take a file for eg. abc and open it then output it as cba.
Seems he wants the whole file reversed, not just each line of the file reversed in and of itself. Well, the two good things about reading from a file one character at a time are: 1. You don't have a problem at the end of file (can't skip past) 2. More importantly you preserve the exact file and file size. When you read with standard token based input you don't know if there is a single space, 20 spaces a tab ... between your tokens. To read things backwards use seek. eg.
|
Author: | TheZsterBunny [ Wed Jun 02, 2004 4:19 pm ] |
Post subject: | |
It strikes me as odd that awesomeJ isn't using a simple 2d array of individual chars rather than all those files. It's pretty cool, the way you've done it and all, but you didn't have to. oh, and mckenzie, when is your lesson on hashing? -Z |
Author: | McKenzie [ Wed Jun 02, 2004 10:14 pm ] |
Post subject: | |
Catch me in the morning on hashing. If not I'll have to pull myself away from Heroclix and do it after school. |