Page 1 of 1

Need urgent help opening .txt files.

Posted: Tue Aug 20, 2013 4:45 pm
by BokBok
Hello. I need urgent help. I need to edit my code by tomorrow so my program allows me to select a .txt document, open it, and place the text from the file in a text field.

The code I have below only opens up the .txt file called numbers on the desktop. I need to change it so it will allow me to open any document.

Code: Select all

global fileChoice
global listOfNumbers
global n
global numbers

on mouseUp
   readInData
   putDataInArray
   displaydata
end mouseUp

on readInData
      open file specialFolderPath("Desktop") & "/numbers.txt" for read
      read from file specialFolderPath("Desktop") & "/numbers.txt" until eof
      put it into listOfNumbers
      read from file file until eof
   close file fileChoice
   end if
end readInData

on putDataInArray
   local i // integer
   put the number of words in listOfNumbers into n
   repeat with i= 1 to n
      put word i of listOfNumbers into numbers[i]
   end repeat
end putDataInArray
displayData
Help is hugely appreciated!

Re: Need urgent help opening .txt files.

Posted: Tue Aug 20, 2013 4:50 pm
by LCNeil
Dear BokBok,

This lesson should be able to help you out-

http://lessons.runrev.com/s/lessons/m/4 ... -text-file

Kind Regards,

Neil Roger
--
RunRev Support Team ~ http://www.runrev.com
--

Re: Need urgent help opening .txt files.

Posted: Tue Aug 20, 2013 5:03 pm
by BokBok
Sorry I am new to livecode and dont understand how to get that to work so it reads the data from inside the text file. Does anyone know what changes I can make to my code so instead of just reading from the file on the desktop it allows me to select the file using answer file?

Re: Need urgent help opening .txt files.

Posted: Tue Aug 20, 2013 5:10 pm
by FourthWorld
Why does it read until EOF twice?

If you're reading the entire file, it may be simpler to use "put url...":

Code: Select all

put url ("file:"& specialFolderPath("Desktop") & "/numbers.txt") into listOfNumbers
But as for why it doesn't work as you have it, hard to say. Have you put in any error checking, looking at the value of "the result" and/or "sysError()" after the open and read commands?

Re: Need urgent help opening .txt files.

Posted: Tue Aug 20, 2013 5:46 pm
by Dixie
Does anyone know what changes I can make to my code so instead of just reading from the file on the desktop it allows me to select the file using answer file?
More or less like Richard said...:-)

Code: Select all

on mouseUp
   answer file "Choose a text file to open" of type "TEXT"
   if it is not empty  then
      put URL ("file:" & it) into fld 1
   end if
end mouseUp