Switch to desktop version  
example of reading a text file on basic - Printable Version

+- Ewon Technical Forum (https://techforum.ewon.biz)
+-- Forum: Development (https://techforum.ewon.biz/forum-50.html)
+--- Forum: BASIC Script (https://techforum.ewon.biz/forum-52.html)
+--- Thread: example of reading a text file on basic (/thread-628.html)



example of reading a text file on basic - jnd - 31-05-2018

Hello,

I have tried some example from documentation for reading data from a text file in basic, but without success.
Is there a full example of code un order to do that?

thank you.


RE: example of reading a text file on basic - simon - 31-05-2018

Hi,

Try this piece of code :

Code:
OPEN "file:/usr/inst_val.txt" FOR BINARY INPUT AS 1
Line$ = ""
ReadNext:
IF EOF 1 THEN GOTO ReadDone
CHAR$ = GET 1,1
IF CHAR$ = CHR$(10) THEN
 PRINT Line$
 //PROCESS LINE
 Line$ = ""
ELSE
 Line$ = Line$ + CHAR$
ENDIF
GOTO ReadNext
ReadDone:
PRINT "close file"
CLOSE 1

PS : Do not forget to adapt the file path --> /usr/inst_val.txt

Simon


RE: example of reading a text file on basic - sheeran - 15-05-2020

(31-05-2018, 04:23 PM)simon Wrote: Hi,

Try this piece of code :

Code:
OPEN "file:/usr/inst_val.txt" FOR BINARY INPUT AS 1
Line$ = ""
ReadNext:
IF EOF 1 THEN GOTO ReadDone
CHAR$ = GET 1,1
IF CHAR$ = CHR$(10) THEN
 PRINT Line$
 //PROCESS LINE
 Line$ = ""
ELSE
 Line$ = Line$ + CHAR$
ENDIF
GOTO ReadNext
ReadDone:
PRINT "close file"
CLOSE 1

PS : Do not forget to adapt the file path --> /usr/inst_val.txt

Simon
   

Hi, Simon,

After we get a Line$, if we want to judge whether that line includes a specific sub-string, then we truncate a specific length of that string, how could we do that with the eWON BASIC IDE syntax?
For instance, I got an API that return a JSON string:
[
   "id": 1,
   "age": 30,
   ...
]

pseudocode:

Code:
IF Line$ includes age THEN
   newCreatedTag@ = Line$.slice(6,7) or Line$.subString(6,7) //get the age 30 out of that line and assign it to new tag
ELSE
   //Do nothing
ENDIF 

Is there a way we can achieve something like this? I flipped the guide and haven't found the proper syntax to do it, any help or advice will be appreciated!

P.S> I'm programming it on the BASIC IDE of the eWON.

Many thanks!


RE: example of reading a text file on basic - simon - 18-05-2020

Yes,
You must use the INSTR function

Pos% = INSTR 1, Line$, "age"
IF Pos% <> 0 THEN
Myvalue$ = Line$(Pos% To X)

By the way, you could also use the little json parse I did : https://techforum.ewon.biz/thread-108.html


RE: example of reading a text file on basic - sheeran - 12-06-2020

(18-05-2020, 05:15 PM)simon Wrote: Yes,
You must use the INSTR function

Pos% = INSTR 1, Line$, "age"
IF Pos% <> 0 THEN
Myvalue$ = Line$(Pos% To X)

By the way, you could also use the little json parse I did : https://techforum.ewon.biz/thread-108.html

Hi, Simon,

Thanks for your reply, it works greatly.

Just one last question, is there a way that I can read a file backward.
There is a CSV file keeps appending new data hourly on client's FTP server, so the length of file stream will keep changing, and I only need its last row. If I can read it backward, the length of previous rows won't affect me that much. Or is it a way to make sure I read the last row of a CSV file in BASIC IDE?

Any help will be appreciated!

Best regards,
Sheeran


RE: example of reading a text file on basic - simon - 16-06-2020

Sheeran,

Where is this file ? On a 3rd party FTP Server ? And you need to retrieve it and read the last row ? Do you have an example ?
To me, in BASIC, you have to read all the file again to finally read the last line. There is no trick I know to start reading from a certain position.


RE: example of reading a text file on basic - sheeran - 16-06-2020

(16-06-2020, 09:18 AM)simon Wrote: Sheeran,

Where is this file ? On a 3rd party FTP Server ? And you need to retrieve it and read the last row ? Do you have an example ?
To me, in BASIC, you have to read all the file again to finally read the last line.  There is no trick I know to start reading from a certain position.

Hi, Simon,

Yes, it locates on the 3rd party FTP server. I fetch the file once an hour, then store it on eWON ftp server. I need to read the last line to get the latest row.

The alternative solution I did: they only reserve last 2 rows in another file, so I am able to separate them accurately without the previous row number interfering, so the problem is resolved.

I still want to know though, if I fetch a csv file externally and store in the ewon ftp server, says /usr/test.csv, what will be the syntax to read the last row only? 

Thanks for your help!
Sheeran


RE: example of reading a text file on basic - simon - 16-06-2020

As I said, you'll then have to use the above script code I gave to read the whole file and only process the last line.
Happy to hear it is solved.

Simon