Author Topic: reading and writing to a file  (Read 6160 times)

0 Members and 1 Guest are viewing this topic.

Offline johnblood

  • Newbie
  • Posts: 15
    • View Profile
reading and writing to a file
« 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

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Shuwatch!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: reading and writing to a file
« Reply #2 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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: reading and writing to a file
« Reply #3 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?


Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: reading and writing to a file
« Reply #4 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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: reading and writing to a file
« Reply #5 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?

Offline johnblood

  • Newbie
  • Posts: 15
    • View Profile
Re: reading and writing to a file
« Reply #6 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.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: reading and writing to a file
« Reply #7 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.

Offline johnblood

  • Newbie
  • Posts: 15
    • View Profile
Re: reading and writing to a file
« Reply #8 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.
« Last Edit: February 25, 2021, 12:39:41 pm by johnblood »

Offline AtomicSlaughter

  • Newbie
  • Posts: 14
    • View Profile
Re: reading and writing to a file
« Reply #9 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.  
« Last Edit: February 25, 2021, 12:59:12 pm by AtomicSlaughter »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: reading and writing to a file
« Reply #10 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.
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: reading and writing to a file
« Reply #11 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 ;-))

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: reading and writing to a file
« Reply #12 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...
« Last Edit: February 25, 2021, 01:04:30 pm by bplus »

Offline AtomicSlaughter

  • Newbie
  • Posts: 14
    • View Profile
Re: reading and writing to a file
« Reply #13 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.  

Offline 191Brian

  • Newbie
  • Posts: 91
    • View Profile
    • My Itch page
Re: reading and writing to a file
« Reply #14 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....
Brian ...