Page 1 of 1

Read from text file and input to address

Posted: Sun Jun 05, 2022 1:28 pm
by iosufdhgd
Looking for a script that reads from a text file, and inputs each line in said text file to a certain address every second or so. Does anything know how to do that?

Re: Read from text file and input to address

Posted: Mon Jun 06, 2022 1:58 pm
by LeFiXER
You want to read a file every second?

Re: Read from text file and input to address

Posted: Mon Jun 06, 2022 2:28 pm
by iosufdhgd
LeFiXER wrote:
Mon Jun 06, 2022 1:58 pm
You want to read a file every second?
same file, just a new line. also, yeah, new line every second would be optimal

Re: Read from text file and input to address

Posted: Mon Jun 06, 2022 3:08 pm
by LeFiXER
You could get the linecount of the file and iterate through the file based on the linecount then update the address according to the position of the loop.

Re: Read from text file and input to address

Posted: Fri Aug 19, 2022 1:17 am
by Yoq209
LeFiXER wrote:
Mon Jun 06, 2022 3:08 pm
You could get the linecount of the file and iterate through the file based on the linecount then update the address according to the position of the loop.
Could you possibly make an example script doing this? I want to apply values from text in a txt.

Re: Read from text file and input to address

Posted: Sun Aug 21, 2022 9:31 pm
by LeFiXER
Yoq209 wrote:
Fri Aug 19, 2022 1:17 am
Could you possibly make an example script doing this? I want to apply values from text in a txt.
It's a relatively trivial task to loop through a file and output the results, nevertheless, I have written a function and provided code on how you can store the data of each line in a table, iterate over that table, and then print the results to the output window:

Code: Select all

function readFile(filename)
  local lines = {}
  -- if file doesn't exist then just return nil because we don't want to proceed
  if filename == nil then return end
  -- open a handle to the file
  local file = io.open(filename)

  -- loop through the file line-by-line
  for line in file:lines() do
      -- Insert the line into the table
      table.insert(lines, line)
  end
  -- close the handle to the file
  file:close()
  return lines
end

-- Initialise a table and store the result of the function as its data
local file_table = readFile('C:\\data.txt')

-- loop through the table and print to Output window
for k,v in pairs(file_table) do
    if v ~= nil then
       print(v)
    end
end