
-----------------------------------
TheXploder
Fri Feb 06, 2004 5:58 pm

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?


var fileNo : int
var fileName : string := "test.txt"
var inputVariable : string (117)

open : fileNo, fileName, read
read : fileNo, inputVariable
close : fileNo

put inputVariable


-----------------------------------
Andy
Fri Feb 06, 2004 6:00 pm


-----------------------------------
y r u using read? thats for binary plus u have to save ur source in the same folder as ur txt file

-----------------------------------
Mazer
Fri Feb 06, 2004 6:01 pm


-----------------------------------
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.

-----------------------------------
Dan
Fri Feb 06, 2004 6:05 pm


-----------------------------------
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:

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


-----------------------------------
TheXploder
Fri Feb 06, 2004 6:10 pm


-----------------------------------
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...

-----------------------------------
Mazer
Fri Feb 06, 2004 6:15 pm


-----------------------------------
OK, but it would still be easier to just use get, especially if that's all you'll be doing with it.

-----------------------------------
TheXploder
Fri Feb 06, 2004 6:20 pm


-----------------------------------
get, doesn't that make a person write something in? and I don't want that...

-----------------------------------
Andy
Fri Feb 06, 2004 6:21 pm


-----------------------------------
no... if u do get:fid,line u get from the txt file

-----------------------------------
TheXploder
Fri Feb 06, 2004 6:26 pm


-----------------------------------
why doesn't this work?


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


-----------------------------------
Dan
Fri Feb 06, 2004 6:32 pm


-----------------------------------
b/c u are not using get right.....see this:

http://www.compsci.ca/v2/viewtopic.php?t=5

-----------------------------------
TheXploder
Fri Feb 06, 2004 6:50 pm


-----------------------------------
ok, I tryed it and I still have difficulty:


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:


1111111111
1000000001
1000000001
1000000001
1000000001
1000000001
1000000001
1000000001
1000000001
1111111111


-----------------------------------
Andy
Fri Feb 06, 2004 7:56 pm


-----------------------------------
whats wrong wit it?

-----------------------------------
TheXploder
Fri Feb 06, 2004 8:02 pm


-----------------------------------
I changed the code a bit, now it works, but exept for the fact that the top line is cut off somehow..:


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


-----------------------------------
Andy
Fri Feb 06, 2004 8:07 pm


-----------------------------------
cuz u got it before the loop

-----------------------------------
TheXploder
Fri Feb 06, 2004 8:09 pm


-----------------------------------
ohh, lol, thx

-----------------------------------
TheXploder
Fri Feb 06, 2004 8:37 pm


-----------------------------------
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..?


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


-----------------------------------
Andy
Sat Feb 07, 2004 12:34 pm


-----------------------------------
the last for loop is useless.. just output it rite after you save it 


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

-----------------------------------
santabruzer
Sat Feb 07, 2004 1:33 pm


-----------------------------------
it's probably string manipulation... or something of the sort... of the array

-----------------------------------
Andy
Sat Feb 07, 2004 1:41 pm


-----------------------------------
oooo... y didnt he just get one character at a time from the txt file?

-----------------------------------
santabruzer
Sat Feb 07, 2004 1:43 pm


-----------------------------------
i have no idea.. and that would be so much simpler and more efficient *decides that he is not gonna write it*

-----------------------------------
TheXploder
Sat Feb 07, 2004 1:44 pm


-----------------------------------
I am going to create a map out of this, so you need the x, y position of every integer..

-----------------------------------
Andy
Sat Feb 07, 2004 1:45 pm


-----------------------------------
yea... 


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

-----------------------------------
TheXploder
Sun Feb 08, 2004 3:34 pm


-----------------------------------
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:


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

