View Full Version : getting a line from text file
sandyrepope
03-03-2007, 21:28
For one of my projects I need to read a line from a text file. The text file will be just a list of words. Of course, each word will be a different length.
After I get the file open, what do I use to get one word from the file?
Thanks
Sandy
Michael Hartlef
03-03-2007, 21:41
So the file has just one line with words, each seperated by a space or comma?
sandyrepope
03-03-2007, 23:01
I hadn't realized just how vague I was about the text file. I'll try to explain.
In the file each word will be on a different line. Like this:
apple
banana
boat
pineapple
color
I'm not sure about what a text editor uses to separate the words. The list of words is created by typing in each word and then pressing enter/return.
This will be for a hangman game where only one of the words will be needed for play. As far as I can see is that the length of each word will be different so I won't be able to use anything that has to have the number of bytes to read. I think this is what is called 'random length records' rather than having a fixed length for each word.
I hope I explained that clearly.
Thanks
Sandy
Michael Hartlef
03-03-2007, 23:07
No problem Sandy,
here is a first example, actually I took the CSV sample from the parse folder and changed it to read the content of a text file called hangman.txt:
uses "file"
uses "console"
dim FileToLoad as string value app_sourcepath & "hangman.txt"
dim MyMatrix() as string
dim nLines as long
dim nCols as long
dim T0, T1 as double
console_writeline("This script demonstrate the use of PARSE function for loading, parsing and filling a string array with the content of a text file. All with only one line of code.")
console_writeline("Input file: " & FileToLoad)
console_writeline("File size : " & file_size(FileToLoad) & " bytes")
console_writeline("Press any key to start.")
console_writeline(repeat$(79, "-"))
console_waitkey
'---
'---Starting to load and parse input file
'------
T0 = timer
'---
'---Just one line do the job of loading file data, parsing text lines, dimensioning and filling the matrix.
'------
PARSE(FILE_Load(FileToLoad), MyMatrix(), $crlf , $CRLF)
'--Now get the number of lines and max number of columns parsed
nLines = ubound(MyMatrix(1))
nCols = ubound(MyMatrix(2))
T1 = timer
'---Write some info
console_writeline("Total loading and parsing time: " & format$(T1 - T0, "#0.000000") & " secs")
console_writeline("Total number of lines : " & nLines)
console_writeline("Total number of colums: " & nCols)
console_writeline("Press any key to show result on screen")
console_writeline(repeat$(79, "-"))
console_waitkey
dim CountLine as long
dim CountCol as long
for CountLine = 1 to nLines
for CountCol = 1 to nCols
console_write(MyMatrix(CountLine, CountCol) & " ")
next
console_writeline("")
next
console_writeline(repeat$(79, "-"))
console_writeline("Program terminated. Press any key to close.")
console_waitkey
If that is to complicated then please let us now.
ErosOlmi
04-03-2007, 00:29
Hi Sandy,
Mike did a great job with his example.
To make it simple, and concentrate on the problem, see this example
uses "file"
dim FileToLoad as string value app_sourcepath & "MyFruitFile.txt"
dim MyArrayOfFruits() as string
PARSE(FILE_Load(FileToLoad), MyArrayOfFruits(), $CRLF)
'---Here MyArrayOfFruits will be an array filled with all the lines found in file
As Mike suggested in another post, I will add some few new functions in FILE module in order to read and write to text files.
Ciao
Eros
Two nice neat examples guys. I will find it handy in the coming days too. Thanks again!
sandyrepope
04-03-2007, 05:36
Thanks, guys. With these two examples I think I can proceed with planning out my program.
Thanks
Sandy