
-----------------------------------
Tallguy
Mon Mar 23, 2009 11:37 am

WHAT!! So Confused
-----------------------------------
Okay so this is my assignment to do. . .

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?

-----------------------------------
A.J
Mon Mar 23, 2009 11:42 am

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
Mon Mar 23, 2009 11:46 am

RE:WHAT!! So Confused
-----------------------------------
which file?

-----------------------------------
A.J
Mon Mar 23, 2009 11:47 am

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.
Mon Mar 23, 2009 11:49 am

RE:WHAT!! So Confused
-----------------------------------
You need:
- [tdoc]File.Exists[/tdoc]
- [tdoc]open[/tdoc]
- [url=http://compsci.ca/v3/viewtopic.php?t=12972]File I/O

-----------------------------------
Tallguy
Mon Mar 23, 2009 11:51 am

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
Mon Mar 23, 2009 11:59 am

RE:WHAT!! So Confused
-----------------------------------
and once i run it, i get '1' for my results

1 of what?????

-----------------------------------
A.J
Mon Mar 23, 2009 12:03 pm

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.

-----------------------------------
Tallguy
Mon Mar 23, 2009 12:07 pm

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
Mon Mar 23, 2009 12:24 pm

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
Tue Mar 24, 2009 12:56 pm

Re: WHAT!! So Confused
-----------------------------------


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 = 1 then
    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"
    end if
else
    put "File does not exist"
end if


okay, so its not opening the file ..

-----------------------------------
DemonWasp
Tue Mar 24, 2009 1:15 pm

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
Tue Mar 24, 2009 1:21 pm

Re: WHAT!! So Confused
-----------------------------------
well, look @ the the file 'wd.txt', i want the contents of it to be displayed

-----------------------------------
Tallguy
Wed Mar 25, 2009 7:42 am

Re: WHAT!! So Confused
-----------------------------------


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 = 1 then
    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
            exit when eof (file)
            get : file, data
            put data :5..
        end loop
    else
        put "Okay"
    end if
else
    put "File does not exist"
end if

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?

    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
Wed Mar 25, 2009 8:15 am

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 ).

-----------------------------------
Tallguy
Wed Mar 25, 2009 8:17 am

RE:WHAT!! So Confused
-----------------------------------
okay it works, thanks man

now, bout checking if there is content inside the file . . . thats wats really confusing me now

-----------------------------------
DemonWasp
Wed Mar 25, 2009 9:07 am

RE:WHAT!! So Confused
-----------------------------------
Well, if the file is empty (exists, but has no contents), when will eof ( file ) be true?

-----------------------------------
SNIPERDUDE
Wed Mar 25, 2009 3:50 pm

RE:WHAT!! So Confused
-----------------------------------
I'd presume instantaneously, or within the first loop.
var num : int := 0

% open file, I forget at the moment how to do this
loop
    exit when eof (file)
    num += 1
    get: file, contents
end loop
% close file

put "Looped " + intstr (num) + " times before closing"

This should output '0', as it will read that there is no (more) data in the file before it can reach the "num += 1" line.

-----------------------------------
saltpro15
Wed Mar 25, 2009 3:56 pm

RE:WHAT!! So Confused
-----------------------------------
SNIPERDUDE you want

var stream : int
open : stream, "file.txt", get
%code
close : stream, "file.txt"


-----------------------------------
Tallguy
Wed Mar 25, 2009 4:24 pm

Re: WHAT!! So Confused
-----------------------------------
okay thanks everyone i got it to work...finally

var answer, filename, data : string
var file : int


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 = 1 then
    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

            if eof (file) = true then
                put "There is no data within the file"
            else
                get : file, data : *
                put data
            end if

            exit when eof (file)

        end loop
    else
        put "Okay"
    end if
else
    put "File does not exist"
end if

close : file



does this make sense, well it works so i guess it has to . .

-----------------------------------
DemonWasp
Wed Mar 25, 2009 5:02 pm

RE:WHAT!! So Confused
-----------------------------------
It makes sense except for one thing - you don't necessarily need "file" to be equal to 1! Anything greater than 0 is a valid stream in Turing, as I recall.

-----------------------------------
Tallguy
Wed Mar 25, 2009 5:37 pm

RE:WHAT!! So Confused
-----------------------------------
oo, umm welll, don't i feel dumb ....

-----------------------------------
DemonWasp
Wed Mar 25, 2009 8:12 pm

RE:WHAT!! So Confused
-----------------------------------
Welcome to Computer Science - where problems always look impossibly hard until you see the solution, then look impossibly easy. We've all felt dumb before (most of us feel dumb on a regular basis still), so don't feel too bad - in fact, feeling dumb on a regular basis is a good sign that you're learning stuff.
