Text Files and OI
Author |
Message |
JayShri
|
Posted: 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.>
Turing: |
procedure reversefile (pString : string)
var stream, stream2 : int
var sWord : string
var file : string
var file2 : string
var c : int
put "Hello. Please enter your file name."
%%IF YOU USE MY EXAMPLE FILE, YOU ENTER MyText2.txt
get file
file2 := file (1 .. * - 4)
open : stream, file, get
c := 0
loop
%input from file
exit when eof (stream )
get : stream, sWord : *
c := c + 1
end loop
open : stream2, file2 + "reversed" + ".txt", put
put : stream2, c, ". ", sWord
close : stream
close : stream2
end reversefile
|
Please specify what version of Turing you are using
4.1
Description: |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
MyText2.txt |
Filesize: |
28 Bytes |
Downloaded: |
137 Time(s) |
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
JayShri
|
Posted: 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...
Turing: |
procedure reversefile (pString : string)
var stream, stream2 : int
var sWord : string
var file : string
var file2 : string
var c, c2 : int
put "Hello. Please enter your file name."
get file
file2 := file (1 .. * - 4)
c2 := 0
loop
exit when c2 = 10
open : stream, file, get, seek
c := 0
loop
%input from file
get : stream, skip
exit when eof (stream )
get : stream, sWord : *
c := c + 1
end loop
open : stream2, file2 + "reversed" + ".txt", put, seek, mod
seek : stream2, *
put : stream2, c, ": ", sWord
c2 := c2 + 1
close : stream
close : stream2
end loop
end reversefile
|
|
|
|
|
|
![](images/spacer.gif) |
np_123
|
Posted: 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:
Turing: |
loop
%input from file
get : stream, skip
exit when eof (stream )
get : stream, sWord : *
c := c + 1
end loop
|
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.
|
|
|
|
|
![](images/spacer.gif) |
JayShri
|
Posted: 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.
Turing: |
loop
%input from file
get : stream, skip
exit when eof (stream )
get : stream, sWord : *
c := c + 1
end loop
|
any suggestions would help...
|
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: 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.
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
|
|