Ewon TechForum
Providing technical solutions to technical requests


example of reading a text file on basic
jnd Offline
Junior Member
#1
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.
Reply

simon Offline
eWON Support
#2
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
Reply

sheeran Offline
Junior Member
#3
(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!
Reply

simon Offline
eWON Support
#4
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
Reply

sheeran Offline
Junior Member
#5
(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
Reply

simon Offline
eWON Support
#6
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.
Reply

sheeran Offline
Junior Member
#7
(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
Reply

simon Offline
eWON Support
#8
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
Reply



Possibly Related Threads…
02-07-2020, 08:49 PM
Last Post: NTA_JK
26-06-2020, 11:50 AM
Last Post: simon
  Console Basic script Started by Cst
1 Replies - 83 Views
24-06-2020, 09:45 AM
Last Post: simon
16-06-2020, 03:03 PM
Last Post: simon
  Redirect via BASIC Script Started by bdoucet
1 Replies - 129 Views
04-06-2020, 02:12 PM
Last Post: simon
02-06-2020, 01:58 PM
Last Post: simon
  How to read a file Started by simon
3 Replies - 302 Views
18-05-2020, 05:16 PM
Last Post: simon
  String type in basic Started by khaterchi
2 Replies - 186 Views
14-05-2020, 07:19 PM
Last Post: khaterchi
12-05-2020, 09:20 AM
Last Post: simon
22-04-2020, 04:09 PM
Last Post: simon



Users browsing this thread:
1 Guest(s)



Theme © Ewon 2019 - Forum software by © MyBB - Cookie policy