Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 File Input (variable) and Appending Files
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Nikestars




PostPosted: Tue Dec 17, 2013 3:06 pm   Post subject: File Input (variable) and Appending Files

What is it you are trying to achieve?
I'm trying to change one part into a file from a string into a real variable. As well as appending a file at the very end(without erasing the file).

What is the problem you are having?
I'm trying to change the file from a string to a real variable. I am most likely over thinking this but I have no idea how to change it. When I import a file it has to be a string (as far as I know). Therefore that file is stored in a variable,but is there a way to change it with a function or am I missing something? I've been struggling on this for a few days now and I can't think of anything. I need it to be changed due to the fact I will be adding the numbers within that file (I know that you cannot add string and int/real together). I've tried converting it from a string to a real variable (strreal) but I've never used this function before and so it didn't work for me.

No clue on how to append files correctly.

Describe what you have tried to solve this problem
I have no clue what to do or how to attempt it. I've tried different things but nothing is working for me. I'm fairly new to file input so this is very different than what I'm use to.

I've attempted to append the file but it says "eof attempted on incompatible stream number 2."
I'm stuck once again.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

var f : int
var staff, compname, employees : string
var sale : string

var employeesales : real
var numberofemployee : int
employeesales := 0
numberofemployee := 0

var highsale,lowsale : real


open : f, "staff.txt", get


put " " : 25, "SALES REPORT"

get : f, compname : *
locate (2, 20)
put compname
put " "
put " " : 15, "SELLER", " " : 21, "SALES"

loop
exit when eof (f)
employeesales := employeesalse + 1
get : f, employees : *
get : f, sale : *
put " " : 15, employees : 31, " ", sale
end loop


locate (12, 16)
put "Total Sold:"
locate (13, 16)
put "Average Sold:"


var streamnumber : int
open : streamnumber, "highestandlowest.txt", put, mod, seek

loop
exit when eof (streamnumber)
seek : streamnumber, *
end loop


put : streamnumber, " "
close : streamnumber



Please specify what version of Turing you are using
4.1.1

Any help is appreciated, THANKS Very Happy Very Happy Very Happy Very Happy
Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: Tue Dec 17, 2013 4:26 pm   Post subject: Re: File Input (variable) and Appending Files

To convert a string to an integer you can use the strintok, strint, strrealok and strreal. Also, if you want to convert an int or real into a string you can use intstr, realstr, erealstr and frealstr.

As for appending to a file you have the right idea, seek to the end of the file then write to it.
You're getting an error because you cannot call eof on a file stream that was not opened for reading (get or read).
To seek to the end of the file, you simply use
Turing:
seek: f, *

without a loop (* indicates to seek to the end of the file).

Also, to make your code easier to read you can wrap your code in the following way
code:
[syntax="Turing"]
... code ...
[/syntax]
Nikestars




PostPosted: Tue Dec 17, 2013 5:39 pm   Post subject: RE:File Input (variable) and Appending Files

But how would the coding look? I understand that I need to use it but what would it look like?
Tony




PostPosted: Tue Dec 17, 2013 6:29 pm   Post subject: RE:File Input (variable) and Appending Files

all of those functions link to the documentation describing their use.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Nikestars




PostPosted: Tue Dec 17, 2013 6:32 pm   Post subject: RE:File Input (variable) and Appending Files

I'm confused on how what to type inside it though

Can you please give me an example on what it would like with numbers and the function of each number? (For the strreal)
Dreadnought




PostPosted: Tue Dec 17, 2013 10:00 pm   Post subject: Re: File Input (variable) and Appending Files

The documentation gives the following example
Turing:
strreal ("2.5e1")

which produces 25.0
So
Turing:
put strreal ("2.5e1") + 3.7

will print 28.7 as an example.
Nikestars




PostPosted: Thu Dec 26, 2013 5:51 pm   Post subject: RE:File Input (variable) and Appending Files

Thanks Smile
np_123




PostPosted: Sun Dec 29, 2013 3:28 pm   Post subject: Re: File Input (variable) and Appending Files

Appending to a file was mentioned and Dreadnought already explained, but I thought I'd add a working example

Turing:


var file : int
var filename : string := "output.txt"

open : file, filename, put, mod, seek     /*  mod (modify) allows you to change the contents of the file  */

seek : file, *         /*  This searches for the end of the file ex. the last character  */

put : file, "This appears at the end of the file"

close : file

Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: