HP Forums
How to read a text file in the App directory? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: How to read a text file in the App directory? (/thread-19922.html)



How to read a text file in the App directory? - mchris - 05-08-2023 07:17 PM

I put a tab delimited file in the app directory, the same place where the logo icon is. I can not find any functions in PPL to read the file. Any help?
Thank you.


RE: How to read a text file in the App directory? - Thomas_Sch - 05-08-2023 07:44 PM

Use AFiles("name")

Each HP Prime app can have any number of files associated with it. These files are sent with the app. For example, if there is a file named icon.png associated with the app, then this file is used for the app icon in the Application Library.
AFiles returns the list of all these files.
AFiles("name") returns the contents of the file with the given name.


(user manual, pg. 658 Chapter 29 Programming in HP PPL)


RE: How to read a text file in the App directory? - mchris - 05-08-2023 08:39 PM

(05-08-2023 07:44 PM)Thomas_Sch Wrote:  Use AFiles("name")

Each HP Prime app can have any number of files associated with it. These files are sent with the app. For example, if there is a file named icon.png associated with the app, then this file is used for the app icon in the Application Library.
AFiles returns the list of all these files.
AFiles("name") returns the contents of the file with the given name.


(user manual, pg. 658 Chapter 29 Programming in HP PPL)

filevar:=AFiles("FILE.txt")
Error: Invalid input

The filevar created but contains the number 0


RE: How to read a text file in the App directory? - Thomas_Sch - 05-08-2023 09:00 PM

(05-08-2023 08:39 PM)mchris Wrote:  
(05-08-2023 07:44 PM)Thomas_Sch Wrote:  Use AFiles("name")

Each HP Prime app can have any number of files associated with it. These files are sent with the app. For example, if there is a file named icon.png associated with the app, then this file is used for the app icon in the Application Library.
AFiles returns the list of all these files.
AFiles("name") returns the contents of the file with the given name.


(user manual, pg. 658 Chapter 29 Programming in HP PPL)

filevar:=AFiles("FILE.txt")
Error: Invalid input

The filevar created but contains the number 0

Sugestion: Do a search in the forum for "Afiles",
e.g. https://www.hpmuseum.org/forum/thread-7726.html?highlight=AFiles
or https://www.hpmuseum.org/forum/search.php?action=results&sid=f5a31b004a8a1b022dab5f4e08300a46&sortby=lastpost&order=desc


RE: How to read a text file in the App directory? - rprosperi - 05-08-2023 09:22 PM

(05-08-2023 08:39 PM)mchris Wrote:  filevar:=AFiles("FILE.txt")
Error: Invalid input

The filevar created but contains the number 0

What object type is 'filevar' ? Probably needs to be string, but your comment saying "created but contains the number 0" implies its numeric of some type??


RE: How to read a text file in the App directory? - Didier Lachieze - 05-08-2023 09:34 PM

You cannot read a text file with AFiles, as AFiles is expecting the file to contain HP Prime objects such as Lists, Strings, Matrix etc... and not raw text characters.
Instead you can use AFilesB to access the text file at byte level.

See some explanations here.


RE: How to read a text file in the App directory? - Jacob Wall - 05-09-2023 03:33 AM

Here is some sample code from one of my apps, cur_name is the name of the text file. Keep in mind that lists are limited to 10000 length, so if reading a file longer than that many bytes, you have to iterate to get the contents.

LOCAL cur_name,fsize,fiter,fcontent,fstring:="",j;

Code:
                  fsize:=AFilesB(cur_name);
                  IF fsize>10000 THEN
                    fiter:=IP(fsize/10000);
                    FOR j FROM 1 TO fiter DO
                      fcontent:=AFilesB(cur_name,(j-1)*10000,10000);
                      fcontent:=CHAR(fcontent);
                      fstring:=fstring+fcontent;
                    END;
                    IF FP(fsize/10000) THEN
                      fcontent:=AFilesB(cur_name,fiter*10000,fsize-(fiter*10000));
                      fcontent:=CHAR(fcontent);
                      fstring:=fstring+fcontent;
                    END;
                    fcontent:=fstring;
                  ELSE
                    fcontent:=AFilesB(cur_name,0,fsize);
                    fcontent:=CHAR(fcontent);
                  END;
                  fcontent:=REPLACE(fcontent,CHAR(13),CHAR(10));
                  fcontent:=REPLACE(fcontent,CHAR(10)+CHAR(10),CHAR(10));



RE: How to read a text file in the App directory? - Tyann - 05-09-2023 06:42 AM

Bonjour,
Une autre possibilité que j'ai déjà utilisé est de copier le contenu du fichier texte dans une note de l'application depuis le logiciel de connectivité sur le PC.
Ensuite vous pouvez récupérer le contenu de cette note donc le texte avec ANote sous forme de chaîne.
Je ne connais pas la limite mais j'ai déjà fais cela avec des fichiers d'une centaine de Ko.
Si ça peut aider.

Hello,
Another possibility I have used is to copy the contents of the text file into a note in the application from the connectivity software on the PC.
Then you can retrieve the content of this note so the text with ANote as a string.
I don't know the limit but I have done this with files of a hundred KB.
If it helps.


RE: How to read a text file in the App directory? - mchris - 05-09-2023 09:56 AM

(05-08-2023 07:44 PM)Thomas_Sch Wrote:  Use AFiles("name")

Each HP Prime app can have any number of files associated with it. These files are sent with the app. For example, if there is a file named icon.png associated with the app, then this file is used for the app icon in the Application Library.
AFiles returns the list of all these files.
AFiles("name") returns the contents of the file with the given name.


(user manual, pg. 658 Chapter 29 Programming in HP PPL)

Thnaks for the reply. You mentioned user manual pg. 658.
Where can I get this? My user guide doesn't have too many pages. It's already in Index by page 640.


RE: How to read a text file in the App directory? - rprosperi - 05-09-2023 12:18 PM

The latest English manual is here:

https://literature.hpcalc.org/items/322