Need urgent help opening .txt files.

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
BokBok
Posts: 2
Joined: Tue Aug 20, 2013 4:34 pm

Need urgent help opening .txt files.

Post by BokBok » Tue Aug 20, 2013 4:45 pm

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!

LCNeil
Livecode Staff Member
Livecode Staff Member
Posts: 1223
Joined: Wed Oct 03, 2012 4:07 pm

Re: Need urgent help opening .txt files.

Post by LCNeil » Tue Aug 20, 2013 4:50 pm

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
--

BokBok
Posts: 2
Joined: Tue Aug 20, 2013 4:34 pm

Re: Need urgent help opening .txt files.

Post by BokBok » Tue Aug 20, 2013 5:03 pm

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?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10062
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Need urgent help opening .txt files.

Post by FourthWorld » Tue Aug 20, 2013 5:10 pm

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?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Need urgent help opening .txt files.

Post by Dixie » Tue Aug 20, 2013 5:46 pm

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

Post Reply