Posted: Mon Mar 23, 2009 11:37 am Post subject: WHAT!! So Confused
Okay so this is my assignment to do. . .
Quote:
1. Create a program which inputs the name of the file to be accessed. The program should use one function to determine whether or not the file can be opened successfully. It should use another function to determine whether or not the file is empty. Save as filechk.t. This program provides a much more elegant manner to determine if a file has opened successfully. It is much better than using assert.
So I don't know what it means, so i asked my teacher, and i'm even more confused. Like what am I suppose to do for this question?
Sponsor Sponsor
A.J
Posted: Mon Mar 23, 2009 11:42 am Post subject: RE:WHAT!! So Confused
You are supposed to use a turing function that check if the file is opened successfully and another function to check if it is empty. Shouldn't be hard at all.
Tallguy
Posted: Mon Mar 23, 2009 11:46 am Post subject: RE:WHAT!! So Confused
which file?
A.J
Posted: Mon Mar 23, 2009 11:47 am Post subject: RE:WHAT!! So Confused
Oh, I see why you are confused. I am assuming that you have to open a text file, and check if it is successfully opened and if it is empty or not.
andrew.
Posted: Mon Mar 23, 2009 11:49 am Post subject: RE:WHAT!! So Confused
Posted: Mon Mar 23, 2009 11:51 am Post subject: RE:WHAT!! So Confused
like would this work?
put "File check program"
put ""
put "What file do you want opened?"
var data : string
var file : int
get data : *
open : file, data, get
put file
**EDIT** providing that there are files in the same place as this file . . .
Tallguy
Posted: Mon Mar 23, 2009 11:59 am Post subject: RE:WHAT!! So Confused
and once i run it, i get '1' for my results
1 of what?????
A.J
Posted: Mon Mar 23, 2009 12:03 pm Post subject: RE:WHAT!! So Confused
'1' is the value stored by the variable to signify that it worked (i.e. 1 -> 'true', whereas 0 -> 'false').
Boolean variables store their values as bits (namely, either '0' or '1'). You got a '1' returned, so it worked.
@andrew - you are only supposed to help him, not give him the answers. Next time, I'll report your post.
Sponsor Sponsor
Tallguy
Posted: Mon Mar 23, 2009 12:07 pm Post subject: RE:WHAT!! So Confused
okay, thanks, so wat function would i use to check if there is any data inside the file or not?
and, once it sees that the the file can be opened can i open it afterwards?
A.J
Posted: Mon Mar 23, 2009 12:24 pm Post subject: RE:WHAT!! So Confused
Of course you can. However, I won't tell you what the function is. Try looking for it in the help files in turing.
Tallguy
Posted: Tue Mar 24, 2009 12:56 pm Post subject: Re: WHAT!! So Confused
Turing:
put"File check program" put"" put"What file do you want opened?...(include file type, .txt/.exe etc)" var answer, filename :string var file :int get filename :*
open: file, filename, get var filevar :string
if file =1then put"File can be exists" put"" put"Do you wish to open the file? Y/N" get answer
if answer ="Y"or answer ="y"then open: file, filename, put put: file,filevar
else put"Okay" endif else put"File does not exist" endif
okay, so its not opening the file ..
DemonWasp
Posted: Tue Mar 24, 2009 1:15 pm Post subject: RE:WHAT!! So Confused
What makes you think it's not opening the file? What output do you get, and what were you expecting? Note that what you're putting into the file is "filevar", which isn't initialised (and therefore will be an empty string in Turing, as I recall).
Tallguy
Posted: Tue Mar 24, 2009 1:21 pm Post subject: Re: WHAT!! So Confused
well, look @ the the file 'wd.txt', i want the contents of it to be displayed
Posted: Wed Mar 25, 2009 7:42 am Post subject: Re: WHAT!! So Confused
Turing:
var answer, filename :string var file :int var data :string put"File check program" put"This program will open any text files" put"" put"What file do you want opened?...(include file type, .txt/.exe etc)"
get filename :*
open: file, filename, get
if file =1then put"File exists" put"" put"Do you wish to open the file? Y/N" get answer
if answer ="Y"or answer ="y"then loop get: file, skip exitwheneof(file) get: file, data
put data :5.. endloop else put"Okay" endif else put"File does not exist" endif
close:file
EDIT - FIXED - okay so i got it to work for any text file, how would i fix the spacing issues once the text is loaded?
Turing:
put data, " " ..
now my problem is how to check if there is data within the choosen files, can someone point me in the right direction?
DemonWasp
Posted: Wed Mar 25, 2009 8:15 am Post subject: RE:WHAT!! So Confused
The spacing issue is because you're outputting with :5 - meaning 5 spaces per word. What you really want to do is get entire lines at a time ( get : file, data : * ) and then output them as entire lines ( put data ).