How to edit files that you open onto a turing run window (text files)
| Author |
Message |
Superskull85
|
Posted: Fri Nov 06, 2009 5:19 pm Post subject: Re: How to edit files that you open onto a turing run window (text files) |
|
|
I think your main problem is that you are outputting the characters instantly after they are pressed. What I think you should do instead is use the variable "text_file" and output the contents of that each iteration of your main loop.
Every time a character is processed you call the "backspace" procedure which locates the cursor one column to the right, and adds the character to "text_file." Instead of doing something like:
| Turing: | loop
Input.Pause
Input.KeyDown (chars )
if chars ('a') then
if chars (KEY_SHIFT ) then
put "A" ..
characteres := "A"
backspace
else
put "a" ..
characteres := "a"
backspace
end if
end if
.. .
end loop |
You could do something like:
| Turing: | loop
Draw.Box (0, 0, maxx, maxy - 20, white) %Used instead of cls () so that your menu bar is not cleared
locatexy (0, maxy - 32) %Same as locate, except it excepts coordinates instead of rows and columns
put text_file .. %Ouput the entire file at once
Input.Pause
Input.KeyDown (chars )
if chars ('a') then
if chars (KEY_SHIFT ) then
characteres := "A"
else
characteres := "a"
end if
text
end if
.. .
end loop |
Using something like that you would be able to delete a character simply by using the code:
| Turing: | text_file := text_file (1 .. * - 1) |
I think that would be a better way of outputting content. Also you should look into using dynamic (flexible) arrays of characters instead of a string variable as it would allow for as many characters as you want. The declaration of a dynamic char array is:
| Turing: | var MyArray : flexible array 1 .. 1 of char %You can use an upper bound greater than 1 if you want
|
When you want to add a new element to that array you would use:
| Turing: | new MyArray, upper (MyArray ) + 1 |
You can find a tutorial about arrays (of all kind) here: http://compsci.ca/v3/viewtopic.php?t=14333
You can also use these escape characters to indicate the following:
Each of them acts as a character so you could still use "text_file := text_file (1 .. * - 1)" to delete a tab or newline if you use them instead of spaces and relocation.
If you are not allowed to use this method than you could simply use "y := y - 1" to make the cursor go back one space. You also need to call "locate (x, y)" within your loop, instead of outside of it.
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
efan41
|
Posted: Tue Nov 10, 2009 3:07 pm Post subject: Re: How to edit files that you open onto a turing run window (text files) |
|
|
I don't understand how just doing MyArray + 1 would remember the inputed letters though Is that all I need or..
By the way, my teacher openely admited to me that I know more about Turing than she does. Her knowlege only goes up to GUI's and procedures... So you guys are my only chance of understanding any of this... lmao.
|
|
|
|
|
 |
efan41
|
Posted: Fri Nov 13, 2009 2:43 pm Post subject: Re: How to edit files that you open onto a turing run window (text files) |
|
|
Superskull87 I tried using your dinamic arrays theorie and it works fine until I actually need to save, that's when it tells me that my variable has no value... Here's what I have:
| code: |
if chars ('f') then %If the f key was pressed
if chars (KEY_SHIFT) then %If the shift key was also pressed
character (t) := "F" %character (for whatever value of t) is now a capital F
backspace %Used to re-arrange x,y coordinates
else %Otherwise
character (t) := "f" %Character (for whatever value of t) is now a normal f
backspace %Used to re-arrange x,y coordinates
end if
end if
|
That repaeats itself for every key on the keyboard, now at the end of this procedure, I have:
| code: |
put character (t) %Puts the letter for the current value of t
t := t + 1 %Adds one to t so it dosen't save over the last letter
new character, t %Frees up character for the new value of t
|
And when I save I have:
| code: |
get DestFile
DestFile += Extension
FileDir += DestFile
if File.Exists (DestFile) then
cls
put "A file already exists under that name. Hit 1 to overwrite"
get Choice
if Choice = 1 then
open : stream, DestFile, put
for c : 1 .. t
put : stream, character (c)
end for
close : stream
exit
else
exit
end if
else
cls
put "No file was found under that name, therefore this document will be saved under the name previously entered."
DestFile += Extension
FileDir += DestFile
open : stream, DestFile, put
for c : 1 .. t
put : stream, character (c)
end for
close : stream
end if
|
Can you tell me what I'm doing wrong because I keep getting a variable has no value for my character (c) when saving...
|
|
|
|
|
 |
Superskull85
|
Posted: Fri Nov 13, 2009 4:59 pm Post subject: RE:How to edit files that you open onto a turing run window (text files) |
|
|
It is because the character at index t (the maximum index in the array) will always be empty (not having a value) until you assign it a value, which you would not if nothing else was entered. You are currently iterating between 1 and t. Instead if you iterate between 1 and (t - 1) than you should be able to save with no problems.
So for your loop. Instead of:
| Turing: | for c : 1 .. t
put : stream, character (c)
end for |
Have:
| Turing: | for c : 1 .. t - 1
put : stream, character (c)
end for |
I attached the program I created a while ago. I did not provide the source code, as I used some modules I had created for large amounts of text, and input, that I don't want to release. Press the escape key to exit the program.
| Description: |
|
 Download |
| Filename: |
TextEditor.zip |
| Filesize: |
280.22 KB |
| Downloaded: |
81 Time(s) |
|
|
|
|
|
 |
efan41
|
Posted: Mon Nov 16, 2009 2:15 pm Post subject: Re: How to edit files that you open onto a turing run window (text files) |
|
|
Wow I can actually save now!!! thanks Superskull. Now I have to write the code to be able to open those dynamic array files lmao.
I tried your text editor btw, why can't I hit the same key twice in a row? Maybe a little bug to work out.
|
|
|
|
|
 |
efan41
|
Posted: Mon Nov 16, 2009 2:28 pm Post subject: Re: How to edit files that you open onto a turing run window (text files) |
|
|
| And this is just something that has nothing to do with the subject but is there a code I can use to delete the very first screen that appears when I run the program? I don't know if you've ever noticed it when you ran my program but I don't want it to show up when I generate a stand alone program out of this...
|
|
|
|
|
 |
Superskull85
|
Posted: Mon Nov 16, 2009 6:26 pm Post subject: RE:How to edit files that you open onto a turing run window (text files) |
|
|
To solve the two windows opening remove the "cls" line from your "debut" procedure. When you draw anything it is sent to the default window (the other window you see) unless you created a window and activated it (which you did do, but you did it after calling "cls").
In effect by calling "cls" Turing automatically opened a window for you, and you than opened a new window in your actual program.
Also for some reason when I compile my text editor editor it will not allow the user to enter the same character in a row, but not when I run the program directly from source code. I don't know why this happening, so sorry about that.
If you're wondering, I limited my editor to non-repeating (holding the button down) characters as I used my own input method (I did not use Input.Pause ()), and because it was just a proof-of-concept (POC) I didn't want to waste time getting the "wait" times worked out. I know it's possible as I did a similar thing for a different program, I just did not bother adding it in, as I had no practical use for the POC text editor I created). Using a simple delay would make the input too slow or too fast, so I didn't bother with it.
|
|
|
|
|
 |
efan41
|
Posted: Thu Nov 19, 2009 2:05 pm Post subject: Re: How to edit files that you open onto a turing run window (text files) |
|
|
Well thanks for all the help with the program, but I think I'm going to mark this as answered now, even if more than the original question have been answered! And btw, you were right about the cls. I removed it and the program works fine now! I'll be sure to post my project into the turing submissions when I hand it in to my teacher.
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
efan41
|
Posted: Thu Nov 19, 2009 2:44 pm Post subject: Re: How to edit files that you open onto a turing run window (text files) |
|
|
Ok I'm going to unmark this as answered...
Why is it that before I turned my normal string variable into a dynamic array I could open the files fine but now that I have an array I can't?
I think it's because the program isn't saving correctly, I get the error code uknown error #0, and the error code can only be obtained in my program when the file doesn't exsist...
When I look into my memory card where it should be saving, the file isn't there, so it obviously can't load something that hasn't saved.
This is what I have for saving:
| Turing: |
winIDsave := Window.Open ("position;0:620,graphics:1000;660")
Window.SetActive (winIDsave )
loop
put "Entrer le nom sous lequel vous voulez enregistrer le fichier."
get DestFile %gets the name of the file they want to save under
DestFile + = Extension %Gives the name an extension, in this case .t
FileDir + = DestFile %Gives the file a directory, which is E:/
if File.Exists (DestFile ) then %If the file exists
cls
put "Un fichier exist deja sous se nom la. Voulez-vous le remplacer avec ce fichier? 1 = oui" %Do you want to overwrite this file
get Choice
if Choice = 1 then
open : stream, DestFile, put
for c : 1 .. t
get : stream, character (c )
put : stream, character (c )
end for
close : stream
exit
else
exit
end if
else
cls %If there isn't a file under that name
put "Aucun fichier a ete trouver sous ce nom. Le fichier sera enregistrer sous le nom inscris."
DestFile + = Extension %Gives the name an extension, in this case .t
FileDir + = DestFile %Gives the file a directory, which is E:/
open : stream, DestFile, put
assert stream > 0
for c : 1 .. t - 1
put : stream, character (c ) ..
end for
close : stream
end if
exit
end loop
delay (4000)
Window.Close (winIDsave )
Window.SetActive (winIDmain )
|
I'm really frustrated lol, it used to work and now it dosen't...
|
|
|
|
|
 |
Superskull85
|
Posted: Thu Nov 19, 2009 10:21 pm Post subject: RE:How to edit files that you open onto a turing run window (text files) |
|
|
Well the only part that I see wrong with the code you provided is in this section:
| Turing: | open : stream, DestFile, put
for c : 1 .. t
get : stream, character (c)
put : stream, character (c)
end for
close : stream |
Turing should spit out an error when it reaches your get statement as you have not added a "get" flag to your open statement (ie. instead of "open : stream, DestFile, put" it should be "open : stream, DestFile, put, get". Also I believe that whenever you open a file with "put" the files contents will be deleted.
Also in that same section you are going from 1 to t (I am assuming t is the upper bound of your array). Instead to prevent iterating to the upper bound (the element that does not have a value) you would have to iterate from 1 to (t - 1). You did this in this section:
| Turing: | open : stream, DestFile, put
assert stream > 0
for c : 1 .. t - 1
put : stream, character (c) ..
end for
close : stream |
I could help more if you wouldn't mind providing your code. PM it to me if you do not want to post it.
|
|
|
|
|
 |
mirhagk
|
Posted: Fri Nov 20, 2009 9:55 am Post subject: RE:How to edit files that you open onto a turing run window (text files) |
|
|
One thing, why do you have this
if chars ('a') then
if chars (KEY_SHIFT) then
characteres := "A"
else
characteres := "a"
end if
text
end if
If you just have chars add to the characteres then it will be capitalized if it has to
|
|
|
|
|
 |
efan41
|
Posted: Fri Nov 20, 2009 2:17 pm Post subject: Re: How to edit files that you open onto a turing run window (text files) |
|
|
ok well the t-1 was just something I forgot.
And as for (stream, DestFile, put, get), I tried it and I still get the same error. But like I said, It can only give me an error code if the file I'm trying to open dosen't exist, so I can deduce that the file hasn't been saved, although I had tried to previously. So, there has to be something wrong in the code I copied in my last post, but I can't see what it could be.
|
|
|
|
|
 |
Kharybdis

|
Posted: Fri Nov 20, 2009 2:56 pm Post subject: RE:How to edit files that you open onto a turing run window (text files) |
|
|
Well, just do a simple error check.
if the file you're trying to open isn't there, make the computer say that it's not there, instead of just guessing.
If the error is something different than from a missing file, then post again.
|
|
|
|
|
 |
efan41
|
Posted: Fri Nov 20, 2009 3:05 pm Post subject: Re: How to edit files that you open onto a turing run window (text files) |
|
|
ok now I'm back to my get attempted on incompatible stream number 3 is back, and I don't know why... I have t-1 and everything.
And would you mind quickly explaining why you use t-1, my teacher dosen't seem to know why, and I can't explain it to her
thanks in advance
|
|
|
|
|
 |
Superskull85
|
Posted: Fri Nov 20, 2009 9:32 pm Post subject: RE:How to edit files that you open onto a turing run window (text files) |
|
|
Well consider the following piece of code:
| Turing: | %Declare a flexible array to hold our text in
var Characters : flexible array 1 .. 1 of char
%Used to keep track of the upper bound of our array
var t : int := 1
%Takes in a character, adds it to our array,
%and adds one to the upper bound of our array
%so that we can add more characters
proc AddChar (InputChar : char)
Characters (t ) := InputChar
t + = 1
new Characters, t
end AddChar
%Retrieve the character at the given index
function GetChar (Index : int) : char
result Characters (Index )
end GetChar
%Displys each character in our array one by one
proc PrintChars ()
for i : 1 .. t - 1
put GetChar (i ) ..
end for
end PrintChars
%Add the characters A-Z to our array
for i : 0 .. 25
AddChar (chr (65 + i ))
end for
%Output our array to the screen
PrintChars ()
|
Because you are using a flexible array to store your text in you always have to add one to the array in order to add more characters. Using this method, the upper most bound is never given a value. Thus when you try to access that element Turing will spit out an error saying that it has no value (because it doesn't). To avoid think you have to iterate to the upper bound minus 1 (t - 1) instead of the upper bound (t).
I would do what Kharybdis suggested to help solve your problem. Posting your full code (or you can send me a PM with the code if you do not want to post it) would help determine the problem.
|
|
|
|
|
 |
|
|