QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: johnblood on February 25, 2021, 12:08:51 pm

Title: reading and writing to a file
Post by: johnblood on February 25, 2021, 12:08:51 pm
Hi,

I'm doing my best to learn QB64 by writing little apps. One of the things I'm working on is a terminal-based (because I'm still learning) ordering app. Basically, (ha) the customer would be asked for their information (name, address, phone) and then can pick the items that they want.  The app would then total everything up and give them a total.

I'm wondering how I would write their information and order to a file? Preferably a txt or csv file.

Thanks
Title: Re: reading and writing to a file
Post by: SpriggsySpriggs on February 25, 2021, 12:15:08 pm
Check these wiki links:

https://qb64.org/wiki/OPEN (https://qb64.org/wiki/OPEN)
https://www.qb64.org/wiki/PUT (https://www.qb64.org/wiki/PUT)
https://www.qb64.org/wiki/PRINT_(file_statement) (https://www.qb64.org/wiki/PRINT_(file_statement))
http://www.qb64.org/wiki/PRINT_USING_(file_statement) (http://www.qb64.org/wiki/PRINT_USING_(file_statement))
Title: Re: reading and writing to a file
Post by: SMcNeill on February 25, 2021, 12:18:05 pm
Easiest file method to start with is PRINT and INPUT.

OPEN “UserName.txt” FOR OUTPUT AS #1
PRINT #1, “Joe Smoe”
PRINT #1, “123 Not Here LN”
PRINT #1, “Nowhere, NS 98765-4321”
CLOSE

And the above has written your data to a file. 

To read it back, open the file for INPUT and use LINE INPUT #1, variable$ to read that information back into your program.
Title: Re: reading and writing to a file
Post by: bplus on February 25, 2021, 12:23:52 pm
Yeah, guys but what he really wants is a Database?

Does he want to toy around in Basic building his own (which we luv of course) or does he want to go professional and not reinvent the wheel and get job done right by the thousands who worked through this and built serious and dependable tools?

Title: Re: reading and writing to a file
Post by: SMcNeill on February 25, 2021, 12:26:31 pm
Yeah, guys but what he really wants is a Database?

Does he want to toy around in Basic building his own (which we luv of course) or does he want to go professional and not reinvent the wheel and get job done right by the thousands who worked through this and built serious and dependable tools?

From this: “I'm doing my best to learn QB64 by writing little apps....”   I’d assume he’d want to start at the very basics of input/output interaction, with fully integrated database support being something which comes later down the line.
Title: Re: reading and writing to a file
Post by: bplus on February 25, 2021, 12:27:21 pm
@SpriggsySpriggs

Whatever happened to that tut to MySQR err, MySQL ?

Too much? weren't you looking for some project awhile ago?
Title: Re: reading and writing to a file
Post by: johnblood on February 25, 2021, 12:28:21 pm
Yeah, guys but what he really wants is a Database?

I'm not ready to start working with a database yet. Simply writing to a file is all I want to do now.

From this: “I'm doing my best to learn QB64 by writing little apps....”   I’d assume he’d want to start at the very basics of input/output interaction, with fully integrated database support being something which comes later down the line.

Exactly.
Title: Re: reading and writing to a file
Post by: bplus on February 25, 2021, 12:31:15 pm
From this: “I'm doing my best to learn QB64 by writing little apps....”   I’d assume he’d want to start at the very basics of input/output interaction, with fully integrated database support being something which comes later down the line.

Well while you tell about first steps, I tell about the road this step will take him down.
Title: Re: reading and writing to a file
Post by: johnblood on February 25, 2021, 12:35:37 pm
A part of my day job is working with a shopping cart and customer management system. So, it kinda makes sense to learn BASIC my recreating what I use everyday. I don't plan for it to ever be used in real life. Just a fun experiment.

Well while you tell about first steps, I tell about the road this step will take him down.

Sounds good to me.
Title: Re: reading and writing to a file
Post by: AtomicSlaughter on February 25, 2021, 12:35:54 pm
If you use the write command you can create a basic CSV file and load them back easier

Code: QB64: [Select]
  1. start
  2.  
  3. SUB start
  4.     DO
  5.         CLS
  6.         INPUT "[1]Read or[2] Write >", rw
  7.         IF rw = 2 THEN rw = 0: Data_Entry
  8.         IF rw = 1 THEN rw = 0: Read_Data
  9.     LOOP
  10.  
  11. SUB Data_Entry
  12.     INPUT "please enter a filename>", filename$
  13.     OPEN filename$ FOR APPEND AS #1
  14.     INPUT "name >", n$
  15.     INPUT "address >", a$
  16.     INPUT "Tel. No. >", t$
  17.     WRITE #1, n$, a$, t$
  18.     CLOSE #1
  19.  
  20.  
  21. SUB Read_Data
  22.     INPUT "Enter a filename >", filename$
  23.     INPUT "Enter a name to search for >", search$
  24.     OPEN filename$ FOR INPUT AS #1
  25.     CLS
  26.     DO
  27.         INPUT #1, n$, a$, t$
  28.         IF n$ = search$ THEN
  29.  
  30.             PRINT n$, a$, t$
  31.         END IF
  32.     LOOP UNTIL EOF(1)
  33.     PRINT: PRINT: PRINT "Press any key to continue"
  34.     SLEEP
  35.     CLOSE #1
  36.  
  37.  
  38.  
Title: Re: reading and writing to a file
Post by: SpriggsySpriggs on February 25, 2021, 12:36:12 pm
@SpriggsySpriggs

Whatever happened to that tut to MySQR err, MySQL ?

Too much? weren't you looking for some project awhile ago?

@bplus I'm very busy these days. I still go back to my MySQL code and make sure it looks decent, though. I want to go back through it again since the Wiki code (where most of it came from) looks sloppy. That, and I want to bring it to v1.5 DIM syntax as well as making sure it is OPTION _EXPLICIT compliant. Once that is done then I'll do a tutorial.... When I have time enough to sit down and do it. But, since johnblood didn't ask about doing a database and is a beginner, I'm not going to suggest that he looks into MySQL or INI manager. It's far better for him to get the understandings of I/O first.
Title: Re: reading and writing to a file
Post by: bplus on February 25, 2021, 12:49:34 pm
@bplus I'm very busy these days. I still go back to my MySQL code and make sure it looks decent, though. I want to go back through it again since the Wiki code (where most of it came from) looks sloppy. That, and I want to bring it to v1.5 DIM syntax as well as making sure it is OPTION _EXPLICIT compliant. Once that is done then I'll do a tutorial.... When I have time enough to sit down and do it. But, since johnblood didn't ask about doing a database and is a beginner, I'm not going to suggest that he looks into MySQL or INI manager. It's far better for him to get the understandings of I/O first.

Glad to see you are busy, the devil will have to look somewhere else ;-))

Just want to warn johnblood, the Basic way is all consuming if you have wife and kids and professional career get out now before you take those first steps... they will show you a power that is very seductive...

just saying ;-))
Title: Re: reading and writing to a file
Post by: bplus on February 25, 2021, 01:01:48 pm
A part of my day job is working with a shopping cart and customer management system. So, it kinda makes sense to learn BASIC my recreating what I use everyday. I don't plan for it to ever be used in real life. Just a fun experiment.

Sounds good to me.

Yeah, I know this story, I lived it. That's what we say, just a little fun is all...  ;-))

Next thing you know you are burning the candle from both ends...
Title: Re: reading and writing to a file
Post by: AtomicSlaughter on February 25, 2021, 01:15:12 pm
This is a slightly more refined version of my previous post with error reporting and partial name search, that prints the output of the seach to the screen.

Code: QB64: [Select]
  1. ON ERROR GOTO ErrorLevel
  2.  
  3. start
  4.  
  5. ErrorLevel:
  6. PRINT "File not found"
  7. start
  8.  
  9. SUB start
  10.     DO
  11.         CLS
  12.         INPUT "[1]Read or[2] Write >", rw
  13.         IF rw = 2 THEN rw = 0: Data_Entry
  14.         IF rw = 1 THEN rw = 0: Read_Data
  15.     LOOP
  16.  
  17. SUB Data_Entry 'Asks for a filename for data to be input to
  18.     INPUT "please enter a filename>", filename$
  19.     OPEN filename$ FOR APPEND AS #1
  20.     INPUT "name >", n$
  21.     INPUT "address >", a$
  22.     INPUT "Tel. No. >", t$
  23.     WRITE #1, n$, a$, t$
  24.     CLOSE #1
  25.  
  26.  
  27. SUB Read_Data ' Aks for filename to open and name search paramerters
  28.     INPUT "Enter a filename >", filename$
  29.     OPEN filename$ FOR INPUT AS #1
  30.     INPUT "Enter a name to search for >", search$
  31.     CLS
  32.     DO
  33.         INPUT #1, n$, a$, t$
  34.         IF INSTR(n$, search$) THEN PRINT n$, a$, t$ ' this line seaches for any instance of search$ in the n$ variable
  35.     LOOP UNTIL EOF(1)
  36.     PRINT: PRINT: PRINT "Press any key to continue"
  37.     SLEEP 5
  38.     CLOSE #1
  39.  
Title: Re: reading and writing to a file
Post by: 191Brian on February 25, 2021, 05:48:30 pm
Hi

I would also suggest looking at  the open as random option with fixed length user defined types you can build simple database functions that way.  I am planning on using  that for storing game assets.

Brian....
Title: Re: reading and writing to a file
Post by: bplus on February 25, 2021, 10:38:03 pm
Hi

I would also suggest looking at  the open as random option with fixed length user defined types you can build simple database functions that way.  I am planning on using  that for storing game assets.

Brian....

Yep! I agree that would be the next step after getting some basic file reading and writing practiced.

Open fileName$ for Random Access along with setting up a record by learning UDT = User Defined Type definitions.
Title: Re: reading and writing to a file
Post by: 191Brian on February 26, 2021, 08:01:35 am
BTW UDT's User Defined Types was a revelation to me when I found them going through Terry Ritchie's tutorial http://www.qb64sourcecode.com/ (http://www.qb64sourcecode.com/)

They make the coding much more efficient/easy to read and if you come from a database background they look very familiar.

Without them if you wanted to store say order details (order no, item No, item desc, qty, price) in an array(s) you create one multi dimension array, have to remember which dimension was which field and do type conversions between strings and numbers or create 5 separate arrays for each field.
With UDT you can just create the UDT and an array of the UDT type e.g

Code: QB64: [Select]
  1.  
  2. TYPE ordLine
  3.     ordNo AS INTEGER
  4.     lineNo AS INTEGER
  5.     itemDesc AS STRING * 50 ' Fixed length string
  6.     qty AS SINGLE
  7.     price AS SINGLE
  8.  
  9. REDIM OrderDtl(10) AS ordLine
  10.  
  11.  
  12. OrderDtl(1).ordNo = 156
  13. OrderDtl(1).lineNo = 1
  14. OrderDtl(1).itemDesc = "Some Widget"
  15. OrderDtl(1).qty = 2
  16. OrderDtl(1).price = 2.5
  17.  
  18. PRINT OrderDtl(1).ordNo, OrderDtl(1).lineNo, RTRIM$(OrderDtl(1).itemDesc), OrderDtl(1).qty, OrderDtl(1).qty * OrderDtl(1).price

Brian ...
Title: Re: reading and writing to a file
Post by: SpriggsySpriggs on February 26, 2021, 08:14:28 am
I think everyone is still going beyond the scope of his current project. We don't want to overwhelm him and have him jump into databases and the like before he figures out regular file operations.
Title: Re: reading and writing to a file
Post by: bplus on February 26, 2021, 11:28:29 am
I think everyone is still going beyond the scope of his current project. We don't want to overwhelm him and have him jump into databases and the like before he figures out regular file operations.

NOT(everyone)

Quote
Yep! I agree that would be the next step after getting some basic file reading and writing practiced.

So I am agreeing with you, @SpriggsySpriggs and I bet anyone who disagrees with that is in the minority.
And I think 191B is OK for showing some hints about UDT with a practical example that is in line with OP (= original post or poster)

And further nobody in this thread has suggested otherwise.

It's not like we are talking about API stuff and bringing in all these terms from who knows where or what? ;-))
Title: Re: reading and writing to a file
Post by: SpriggsySpriggs on February 26, 2021, 01:05:16 pm
@bplus
Quote
And further nobody in this thread has suggested otherwise.
I'm just making a statement that we want to keep it extremely basic since he is a beginner. Steve and Atomic Kevin's messages are the two most relevant answers to his question.

Quote
It's not like we are talking about API stuff and bringing in all these terms from who knows where or what? ;-))
Which is exactly why I didn't bring up using the WinAPI. He mentioned he's a beginner and I know that would be asking him to skip many steps.