Computer Science Canada

WHAT!! So Confused

Author:  Tallguy [ 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?

Author:  A.J [ 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.

Author:  Tallguy [ Mon Mar 23, 2009 11:46 am ]
Post subject:  RE:WHAT!! So Confused

which file?

Author:  A.J [ 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.

Author:  andrew. [ Mon Mar 23, 2009 11:49 am ]
Post subject:  RE:WHAT!! So Confused

You need:
- File.Exists
- open
- File I/O

Author:  Tallguy [ 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 . . .

Author:  Tallguy [ 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?????

Author:  A.J [ 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.

Author:  Tallguy [ 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?

Author:  A.J [ 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.

Author:  Tallguy [ 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 = 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 ..

Author:  DemonWasp [ 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).

Author:  Tallguy [ 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

Author:  Tallguy [ 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 = 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?
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?

Author:  DemonWasp [ 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 ).

Author:  Tallguy [ Wed Mar 25, 2009 8:17 am ]
Post subject:  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

Author:  DemonWasp [ Wed Mar 25, 2009 9:07 am ]
Post subject:  RE:WHAT!! So Confused

Well, if the file is empty (exists, but has no contents), when will eof ( file ) be true?

Author:  SNIPERDUDE [ Wed Mar 25, 2009 3:50 pm ]
Post subject:  RE:WHAT!! So Confused

I'd presume instantaneously, or within the first loop.
Turing:
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.

Author:  saltpro15 [ Wed Mar 25, 2009 3:56 pm ]
Post subject:  RE:WHAT!! So Confused

SNIPERDUDE you want
Turing:

var stream : int
open : stream, "file.txt", get
%code
close : stream, "file.txt"

Author:  Tallguy [ Wed Mar 25, 2009 4:24 pm ]
Post subject:  Re: WHAT!! So Confused

okay thanks everyone i got it to work...finally
Turing:

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 . .

Author:  DemonWasp [ Wed Mar 25, 2009 5:02 pm ]
Post subject:  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.

Author:  Tallguy [ Wed Mar 25, 2009 5:37 pm ]
Post subject:  RE:WHAT!! So Confused

oo, umm welll, don't i feel dumb ....

Author:  DemonWasp [ Wed Mar 25, 2009 8:12 pm ]
Post subject:  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.


: