Results 1 to 7 of 7

Thread: getting a line from text file

  1. #1

    getting a line from text file

    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

  2. #2

    Re: getting a line from text file

    So the file has just one line with words, each seperated by a space or comma?

  3. #3

    Re: getting a line from text file

    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

  4. #4

    Re: getting a line from text file

    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:

    [code=thinbasic] 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
    [/code]

    If that is to complicated then please let us now.

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,782
    Rep Power
    10

    Re: getting a line from text file

    Hi Sandy,

    Mike did a great job with his example.

    To make it simple, and concentrate on the problem, see this example
    [code=thinbasic]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[/code]

    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
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  6. #6
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Re: getting a line from text file

    Two nice neat examples guys. I will find it handy in the coming days too. Thanks again!
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  7. #7

    Re: getting a line from text file

    Thanks, guys. With these two examples I think I can proceed with planning out my program.

    Thanks
    Sandy

Similar Threads

  1. Text Caret To End of Line
    By catventure in forum UI (User Interface)
    Replies: 2
    Last Post: 04-01-2009, 17:27

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •