Creating a text file isn't working for me

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

trags3
Posts: 418
Joined: Wed Apr 09, 2014 1:58 am
Location: Las Vegas, NV

Creating a text file isn't working for me

Post by trags3 » Mon Mar 08, 2021 3:21 pm

Hi again,
I have worked on this for hours now and cannot find what I am doing wrong.

Code: Select all

on openStack
   set the defaultFolder to (specialFolderPath("Documents") & slash)
   put URL("file:" & (specialFolderPath("Documents") & "/PC.txt")) into tfile
end openStack
This code works on iPhone. only difference is I used lowercase documents rather than Documents.

I have tried both ways on my Mac.
The PC.txt file is not created.

Any suggestions?
Tom

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Creating a text file isn't working for me

Post by bogs » Mon Mar 08, 2021 3:27 pm

on openStack
set the defaultFolder to (specialFolderPath("Documents") & slash)
put URL("file:" & (specialFolderPath("Documents") & "/PC.txt")) into tfile
end openStack
Problem I see is that you have a slash, then include the slash in the files name. Me, I just include the slash as part of the name since I find it easier.
Image

glenn9
Posts: 223
Joined: Wed Jan 15, 2020 10:45 pm
Location: Europe

Re: Creating a text file isn't working for me

Post by glenn9 » Mon Mar 08, 2021 3:38 pm

Not sure if its of any help but this seems to work for me...

Code: Select all

...

-- opening a file
set the defaultfolder to specialfolderpath("desktop")
put URL ("file:example.txt") into tSomething

...

-- saving a file
set the defaultfolder to specialfolderpath("desktop")
put tSomething into url ("file:example.txt")
   
...

trags3
Posts: 418
Joined: Wed Apr 09, 2014 1:58 am
Location: Las Vegas, NV

Re: Creating a text file isn't working for me

Post by trags3 » Mon Mar 08, 2021 3:47 pm

Thanks bogs but....

Code: Select all

on openStack
   set the defaultFolder to (specialFolderPath("Documents") )
   put URL("file:" & (specialFolderPath("Documents") & "/PC.txt")) into tfile
end openStack

THis doesn't work either.:cry:

Tom

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Creating a text file isn't working for me

Post by bogs » Mon Mar 08, 2021 3:56 pm

trags3 wrote:
Mon Mar 08, 2021 3:47 pm
set the defaultFolder to (specialFolderPath("Documents") )
Try dropping the outer parenthesis around specialFolder hee hee.
set the defaultFolder to specialFolderPath("Documents")
Image

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Creating a text file isn't working for me

Post by Klaus » Mon Mar 08, 2021 4:21 pm

Hi Tom,

you are supplying an ABSOLUTE pathname, so setting the defaultfolder is not neccessary!
Try this:

Code: Select all

on openStack
   put specialFolderPath("documents") & "/PC.txt") into tfile
   if there is NOT a file tFile then
     answer "Not file yet!"
   else
    put url("file:" & tFile) into fld "content of file"
  end if
end openStack
Best

Klaus

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Creating a text file isn't working for me

Post by Klaus » Mon Mar 08, 2021 4:27 pm

To be clear, THIS:

Code: Select all

put URL("file:" & (specialFolderPath("Documents") & "/PC.txt")) into tFile
will NOT create a file, but rather read that file into the variable tFile or give an ERROR if the file is not present!

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Creating a text file isn't working for me

Post by bogs » Mon Mar 08, 2021 4:30 pm

I must be slow this morning, forgive me. Let's start over here with your initial statement (now that I actually read the title of the post).
Creating a text file isn't working for me
So, everything I said about the default folder isn't going to create a file, to do that, you need 2 things, a path, which is what your building tFile to be, and something to put INTO that path.

This code puts the contents of an existing file into a variable :
put URL("file:" & (specialFolderPath("Documents") & "/PC.txt")) into tfile

To put that variable (or whatever) back into a file, you need to reverse it:
put tfile into URL("file:" & (specialFolderPath("Documents") & "/PC.txt"))

NOW that we're on the same page, are there any other questions hee hee.

LOL I see Klaus beat me too it
Image

trags3
Posts: 418
Joined: Wed Apr 09, 2014 1:58 am
Location: Las Vegas, NV

Re: Creating a text file isn't working for me

Post by trags3 » Mon Mar 08, 2021 5:38 pm

Klaus
This doesn't work.
You are missing a "(" somewhere I put it after the "&" didn't work
I removed the ")" before the into and that didn't work
I put it in front of "special and that doesn't work either

Code: Select all

on openStack
   put specialFolderPath("documents") & "/PC.txt") into tfile
   if there is NOT a file tFile then
     answer "Not file yet!"
   else
    put url("file:" & tFile) into fld "content of file"
  end if
end openStack
I am using LC 9.6.1
Macbook air OS Catalina 10.15.4

Tom

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Creating a text file isn't working for me

Post by Klaus » Mon Mar 08, 2021 5:52 pm

Hi Tom,

yes, sorry for the "(":

Code: Select all

...
## This is correct:
put specialFolderPath("documents") & "/PC.txt" into tFile
...
But what do you mean with -> This doesn't work? 8)
Do you see the dialog in LC?
Do you see the content of that file in your field?
Did your machine explode?
Or what do you think SHOULD happen but didn't?

Best

Klaus

trags3
Posts: 418
Joined: Wed Apr 09, 2014 1:58 am
Location: Las Vegas, NV

Re: Creating a text file isn't working for me

Post by trags3 » Mon Mar 08, 2021 6:30 pm

It still does not work.
There is not a file named PC.txt anywhere on my computer.
Tom

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Creating a text file isn't working for me

Post by Klaus » Mon Mar 08, 2021 6:38 pm

Just to be sure:
specialfolderpath("documents") on desktop is completely different from specialfolderpath("documents") on MOBILE!
They are NOT the same folders! They have nothing in common except the name and the fact that we have write permission
in that folder on desktop and mobile.

trags3
Posts: 418
Joined: Wed Apr 09, 2014 1:58 am
Location: Las Vegas, NV

Re: Creating a text file isn't working for me

Post by trags3 » Mon Mar 08, 2021 6:57 pm

This app isn't going to be on mobile.
Tom

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Creating a text file isn't working for me

Post by Klaus » Mon Mar 08, 2021 7:16 pm

Hi Tom,

well, someone needs to write that file to your specialfolderpaht("documents") before you can load it into your stack!
Did you? If yes, how? If not, who on earth did? :-D

This does NOT create a file:

Code: Select all

on openStack
   put URL("file:" & (specialFolderPath("Documents") & "/PC.txt")) into tfile
end openStack

Best

Klaus

trags3
Posts: 418
Joined: Wed Apr 09, 2014 1:58 am
Location: Las Vegas, NV

Re: Creating a text file isn't working for me

Post by trags3 » Mon Mar 08, 2021 7:35 pm

Hi Klaus
I guess I'm trying to do something that maybe can't be done.
When the app is sent to a user I was under the impression that the app would be able to generate about 9 or 10 empty txt files that will be used when the app is running. As they enter data on a card it is saved in a field on the card. When they skip to the next card the data in the fields get saved in a text file. If the user quits the app before completing all pages of input, those files are used to repopulate the cards with the data they had entered during the prior use of the app.

Is there an easier way to accomplish this? I have done this on other apps in the past that worked on iPhone ,Mac and Windows.

Tom

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”