Computer Science Canada

Loading .txt Files

Author:  TheXploder [ Fri Feb 06, 2004 5:58 pm ]
Post subject:  Loading .txt Files

I got this mostly from the help file, then I made a text file with the name "test.txt" and saved a few words in it, but when I press enter, and add some more words and run the program a strange box char appers, can I get rid of that?

code:

var fileNo : int
var fileName : string := "test.txt"
var inputVariable : string (117)

open : fileNo, fileName, read
read : fileNo, inputVariable
close : fileNo

put inputVariable

Author:  Andy [ Fri Feb 06, 2004 6:00 pm ]
Post subject: 

y r u using read? thats for binary plus u have to save ur source in the same folder as ur txt file

Author:  Mazer [ Fri Feb 06, 2004 6:01 pm ]
Post subject: 

The problem is that you are trying to add text to a binary data file. You could have a program to write to the file in binary, but it would best not to edit the file in notepad or something.

Author:  Dan [ Fri Feb 06, 2004 6:05 pm ]
Post subject: 

i am gussing that whould repprestent a "\n" or a line break. i am not shure what u are doing but i think that it whould be eaer with put and get file i/o unless u are trying to do somting more comapliated then i think.

ex of get/put i/o for files from turing doc:
code:

var word : string
        loop
            get skip        % Skip over any white space
            exit when eof   % Are there more characters?
            get word        % Input next token
            put word        % Output the token
        end loop

Author:  TheXploder [ Fri Feb 06, 2004 6:10 pm ]
Post subject: 

the thing is that I just wanna load strings out of a text file...
I do have them saved in the dame folder... I just want to load it and read, almost like:

var words : array 1..10, 1..10 of string

I don't intend to change the array within the program at all... just load the info I need...

Author:  Mazer [ Fri Feb 06, 2004 6:15 pm ]
Post subject: 

OK, but it would still be easier to just use get, especially if that's all you'll be doing with it.

Author:  TheXploder [ Fri Feb 06, 2004 6:20 pm ]
Post subject: 

get, doesn't that make a person write something in? and I don't want that...

Author:  Andy [ Fri Feb 06, 2004 6:21 pm ]
Post subject: 

no... if u do get:fid,line u get from the txt file

Author:  TheXploder [ Fri Feb 06, 2004 6:26 pm ]
Post subject: 

why doesn't this work?

code:

var fileNo : int
var fileName : string := "test.txt"   % Name of file
var inputVariable : string (117)

open : fileNo, fileName, read
read : fileNo, inputVariable

var word : string

loop
    get skip                % Skip over any white space
    exit when eof           % Are there more characters?
    get inputVariable                % Input next token
end loop

close : fileNo

put inputVariable

Author:  Dan [ Fri Feb 06, 2004 6:32 pm ]
Post subject: 

b/c u are not using get right.....see this:

http://www.compsci.ca/v2/viewtopic.php?t=5

Author:  TheXploder [ Fri Feb 06, 2004 6:50 pm ]
Post subject: 

ok, I tryed it and I still have difficulty:

code:

var temp : string
var stremin ,stremout : int %a var that is an int is needed to open a file

open : stremin, "test.txt", get %this will open a connection to get data

get : stremin, temp

loop
    exit when eof (stremin)
    get : stremin, temp
end loop

put temp

close: stremout


my text file has this in it:

code:

1111111111
1000000001
1000000001
1000000001
1000000001
1000000001
1000000001
1000000001
1000000001
1111111111

Author:  Andy [ Fri Feb 06, 2004 7:56 pm ]
Post subject: 

whats wrong wit it?

Author:  TheXploder [ Fri Feb 06, 2004 8:02 pm ]
Post subject: 

I changed the code a bit, now it works, but exept for the fact that the top line is cut off somehow..:

code:

var map : array 1 .. 10, 1 .. 10 of int
var numbers : string := ""
var number : string := ""
var stremin, stremout : int %a var that is an int is needed to open a file

open : stremin, "test.txt", get %this will open a connection to get data

get : stremin, number

loop
    exit when eof (stremin)
    get : stremin, number
    %numbers := numbers + number
    put number
end loop

put numbers

Author:  Andy [ Fri Feb 06, 2004 8:07 pm ]
Post subject: 

cuz u got it before the loop

Author:  TheXploder [ Fri Feb 06, 2004 8:09 pm ]
Post subject: 

ohh, lol, thx

Author:  TheXploder [ Fri Feb 06, 2004 8:37 pm ]
Post subject: 

I added some more code now it adds the value loaded to a two dimensional array, yay now I'm done my game... because all you need is numbers it's all about numbers every game is made with arrays full of numbers like CS, anyway, can I shorten that any further..?

code:

var map : array 1 .. 10, 1 .. 10 of int
var number : array 1 .. 10 of string
var stremin, stremout : int %a var that is an int is needed to open a file

open : stremin, "test.txt", get %this will open a connection to get data

loop
    exit when eof (stremin)
    for i : 1 .. 10
        get : stremin, number (i)
    end for
end loop

for i : 1 .. 10
    for i2 : 1 .. 10
        map (i, i2) := strint (number (i) (i2))
    end for
end for

for i : 1 .. 10
    for i2 : 1 .. 10
        put map (i, i2) ..
    end for
    put ""
end for

Author:  Andy [ Sat Feb 07, 2004 12:34 pm ]
Post subject: 

the last for loop is useless.. just output it rite after you save it

code:

for i : 1 .. 10
    for i2 : 1 .. 10
        map (i, i2) := strint (number (i) (i2))
        put map (i, i2) ..
    end for
put""
end for



???
strint (number (i) (i2))
number is a 1 d aray

Author:  santabruzer [ Sat Feb 07, 2004 1:33 pm ]
Post subject: 

it's probably string manipulation... or something of the sort... of the array

Author:  Andy [ Sat Feb 07, 2004 1:41 pm ]
Post subject: 

oooo... y didnt he just get one character at a time from the txt file?

Author:  santabruzer [ Sat Feb 07, 2004 1:43 pm ]
Post subject: 

i have no idea.. and that would be so much simpler and more efficient *decides that he is not gonna write it*

Author:  TheXploder [ Sat Feb 07, 2004 1:44 pm ]
Post subject: 

I am going to create a map out of this, so you need the x, y position of every integer..

Author:  Andy [ Sat Feb 07, 2004 1:45 pm ]
Post subject: 

yea...

code:

var temp:string(1)
for i:1..10
    for j:1..10
        get:file, temp
        map(i,j):=strint(temp)
    end for
end for

and he's done

Author:  TheXploder [ Sun Feb 08, 2004 3:34 pm ]
Post subject: 

Dodge, I don't understand how your code works, could you write a complete source that also loads a file..
Anyway I got another probelem I want to open map.txt files that are in another folder:

code:

var map : array 1 .. 10, 1 .. 10 of int
var number : array 1 .. 10 of string
var stremin, stremout : int := 1 %a var that is an int is needed to open a file

open : stremin, "New Folder\map.txt", get %this will open a connection to get data

loop
    exit when eof (stremin)
    for i : 1 .. 10
        get : stremin, number (i)
    end for
end loop

close : stremout

for i : 1 .. 10
    for i2 : 1 .. 10
        map (i, i2) := strint (number (i) (i2))
    end for
end for

for i : 1 .. 10
    for i2 : 1 .. 10
        put map (i, i2)
    end for
end for


: