Computer Science Canada Text Files and OI |
Author: | JayShri [ Thu Oct 23, 2014 4:22 pm ] | ||
Post subject: | Text Files and OI | ||
What is it you are trying to achieve? Writing a procedure called reversefile that accepts the name of an existing text file. The file should be reversed (line by line) to form the output file. Include reversed line numbers in the output text file. (Hint: you will have to open and close the input file several times). Name the output file [FileName]Reversed.txt What is the problem you are having? Right now, it just takes the last line (the 5th line which is "I am Jay"). And puts that in my output file. I'm 90% sure that I need loops to open and close the input file and seeks/mods in there to get it to work. Describe what you have tried to solve this problem Googling how to use seek and mod (didn't get anywhere) and using put and get in my program. Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) <I've attached the example text file that you can use.>
Please specify what version of Turing you are using 4.1 |
Author: | JayShri [ Thu Oct 23, 2014 4:39 pm ] | ||
Post subject: | RE:Text Files and OI | ||
**EDIT** Now my code writes the last line of the existing file 10 times into the new file. I figured out how to use mod and seek . Still can't get it to do what I want it to do...
|
Author: | np_123 [ Fri Oct 24, 2014 5:30 pm ] | ||
Post subject: | Re: Text Files and OI | ||
First point of interest - opening and closing the text files multiple times is completely unnecessary for what you are trying to do Second point of interest - the reason your code outputs the last line multiple times is because of this code you have in there:
That code keeps overwriting the variable sWord and when it finally exits the loop, sWord is assigned to the very last line in the text file. Any other data from the text file is overwritten and stored nowhere. One way of fixing that is by appending to the string when you search the text file. Keep in mind you still have to be able to tell when a new line starts. |
Author: | JayShri [ Sun Oct 26, 2014 8:59 pm ] | ||
Post subject: | RE:Text Files and OI | ||
The thing I need help with is getting my code to get the lines in the right order (or doing whatever I need to do to get it reversed). I fully understand what my code is doing wrong; I inserted counters and loops as place holders until I can get my code right. I just need to know what do do inside this loop to get my text from the file in reverse order.
any suggestions would help... |
Author: | Tony [ Mon Oct 27, 2014 1:13 am ] |
Post subject: | Re: RE:Text Files and OI |
JayShri @ Sun Oct 26, 2014 8:59 pm wrote: any suggestions would help...
I suggest thinking about a much simpler problem -- reversing letters in a single word. E.g. turning "foo" into "oof". What you are trying to solve is this "reverse a word" problem + each letter is actually a full line + have to deal with files. Jumping straight into multiple problems can easily be overwhelming, but those smaller problems give you all of the pieces to put together a full solution. |