Author Topic: Do I need to need a directory path for creating a file?  (Read 5115 times)

0 Members and 1 Guest are viewing this topic.

Offline Donald Foster

  • Newbie
  • Posts: 63
    • View Profile
Do I need to need a directory path for creating a file?
« on: October 27, 2021, 05:18:08 pm »
Hello Everyone,

          It's been over 20 years since I created any sequential or random access files and I didn't have to provide directory path. Is there a preferred directory I should use for storing player game information. I'm planning on creating sequential for player info for my solo version of Lucky Numbers. I'm thinking of using a max. of 8 player's names and the level their currently on that way different household members may be sharing a computer, they can save and load their game by their name similar to DOOM where the unused spaces will display EMPTY. Or am I going down the wrong path and is there a better method?

Thanks, Donald   

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Do I need to need a directory path for creating a file?
« Reply #1 on: October 27, 2021, 06:04:41 pm »
Yeah sure, keep the data file(s) in same folder as the source and exe, no path needed.

Haven't you used image files or sound files with your games?

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Do I need to need a directory path for creating a file?
« Reply #2 on: October 27, 2021, 06:15:05 pm »
You don't need a separate path, but you may want to make a separate game folder, and save your program and scoring files in that directory. However, if you decide you do want a separate directory folder, inside your local game folder, you can either use the full path, like C:\qb64|mygame\scores\ or just the relative path, scores\ in the file access statement. You can also use QB64 keywords like _CWD$ to find the path you are running in, if you want to use a full path call.

Anyway, I use RA files, so I can overwrite scores. You may want to take a look at my ASCII Invaders game, in the Competition Discussion thread. Sequential files are fine, but you generally have to load the whole file into an array, change the arrays based upon game results, and then open the file for OUTPUT and write the arrays back into the file. I suppose if you don't want to use fields or make a fixed length file, as used in RA files, sequential is your best bet.

Pete 
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Donald Foster

  • Newbie
  • Posts: 63
    • View Profile
Re: Do I need to need a directory path for creating a file?
« Reply #3 on: October 27, 2021, 06:57:37 pm »
Thanks for the advice. I never made an RA file, but I'll look into it.

thanks, Donald

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Do I need to need a directory path for creating a file?
« Reply #4 on: October 27, 2021, 07:18:41 pm »
I did a Top 10 tracker a couple years ago. Just checked it out and fixed for QB64 v2.0
https://www.qb64.org/forum/index.php?topic=1511.msg107586#msg107586

Code's all done for you, if the function returns an empty string then the player wants to play Again. I combined Top Ten tracking with Go Again question. Just call the function with the score.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Do I need to need a directory path for creating a file?
« Reply #5 on: October 27, 2021, 07:38:24 pm »
Anyway, I use RA files, so I can overwrite scores. You may want to take a look at my ASCII Invaders game, in the Competition Discussion thread. Sequential files are fine, but you generally have to load the whole file into an array, change the arrays based upon game results, and then open the file for OUTPUT and write the arrays back into the file. I suppose if you don't want to use fields or make a fixed length file, as used in RA files, sequential is your best bet.
Pete

You can do the same thing with Binary files too, just tell it the byte to start on in the GET and PUT statements.

PUT #1, Bytes_in_Record*X, Record

where X is the number of the Record you want to over write.
Granted after becoming radioactive I only have a half-life!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Do I need to need a directory path for creating a file?
« Reply #6 on: October 27, 2021, 09:53:50 pm »
@Cobalt Love to see a binary example, if you have one.

@Donald Foster  Here is a stripped down no-frills top 5 scoring RA files method I used in my ASCII Invaders game.

It makes a file called: ascii-invaders-high-score.dat

Code: QB64: [Select]
  1. DIM SHARED score$
  2. LINE INPUT "Score: "; score$
  3. IF LEN(score$) <> 6 THEN BEEP: RUN
  4.  
  5. CALL displayhighscores
  6.  
  7.  
  8.  
  9. SUB displayhighscores
  10.  
  11.     DIM hs AS STRING * 25
  12.     REDIM highscore$(6), hsdata$(6)
  13.     DO
  14.         IF _FILEEXISTS("ascii-invaders-high-score.dat") THEN
  15.             OPEN "ascii-invaders-high-score.dat" FOR RANDOM AS #1 LEN = 25
  16.             FOR i = 1 TO 5
  17.                 GET #1, i, hs
  18.                 highscore$(i) = MID$(hs, 10, 6): hsdata$(i) = hs
  19.             NEXT
  20.             CLOSE #1
  21.         ELSE
  22.             FOR i = 1 TO 5
  23.                 hsdata$(i) = SPACE$(25)
  24.             NEXT
  25.         END IF
  26.  
  27.         IF VAL(score$) > VAL(highscore$(5)) THEN
  28.  
  29.             IF VAL(score$) > VAL(highscore$(1)) THEN
  30.                 msg$ = " <<< HIGH SCORE / Enter Your Initials >>> "
  31.             ELSE
  32.                 msg$ = " <<< Top 5 Score. Enter Your Initials >>> "
  33.             END IF
  34.  
  35.             GOSUB hiscore
  36.  
  37.             LOCATE 4, 41 - LEN(msg$) \ 2
  38.             PRINT msg$;
  39.  
  40.             bxy% = 6
  41.             bxx% = 26
  42.             t$ = " " + CHR$(218) + STRING$(27, CHR$(196)) + CHR$(191) + " "
  43.  
  44.             LOCATE bxy%, bxx% - 1: PRINT t$;
  45.  
  46.             FOR i = 1 TO 11
  47.                 t$ = " " + CHR$(179) + STRING$(27, CHR$(32)) + CHR$(179) + " "
  48.                 LOCATE bxy% + i, bxx% - 1: PRINT t$;
  49.             NEXT
  50.  
  51.             t$ = " " + CHR$(192) + STRING$(27, CHR$(196)) + CHR$(217) + " "
  52.  
  53.             LOCATE bxy% + i, bxx% - 1: PRINT t$
  54.  
  55.             t$ = "    NAME   SCORE    DATE   "
  56.  
  57.             LOCATE bxy% + 1, bxx% + 1: PRINT t$
  58.  
  59.             FOR i = 1 TO 5
  60.                 t$ = hsdata$(i)
  61.                 LOCATE bxy% + 1 + i * 2, bxx% + 2: PRINT t$
  62.             NEXT
  63.  
  64.             DO
  65.                 LOCATE 7 + rank * 2, bxx% + 6: PRINT "   ";
  66.                 LOCATE 7 + rank * 2, bxx% + 6: LINE INPUT ; initials$
  67.                 IF LEN(initials$) = 3 THEN EXIT DO
  68.                 BEEP
  69.             LOOP
  70.  
  71.             hsname$ = initials$
  72.  
  73.             MID$(hsdata$(rank), 5, 3) = hsname$ + SPACE$(3 - LEN(hsname$))
  74.             OPEN "ascii-invaders-high-score.dat" FOR RANDOM AS #1 LEN = 25
  75.             FOR i = 1 TO 5
  76.                 hs = hsdata$(i)
  77.                 IF LEFT$(hs, 1) = "" THEN MID$(hs, 1, 2) = "0" + LTRIM$(STR$(i))
  78.                 PUT #1, i, hs
  79.             NEXT
  80.             CLOSE #1
  81.  
  82.             EXIT DO
  83.         ELSE
  84.             EXIT DO ' Not in the top 5 highest scores so exit sub.
  85.         END IF
  86.     LOOP
  87.     EXIT SUB
  88.  
  89.     hiscore:
  90.     FOR i = 1 TO 5
  91.         IF VAL(score$) > VAL(highscore$(i)) THEN rank = i: EXIT FOR
  92.     NEXT
  93.  
  94.     hsdata$(6) = SPACE$(25)
  95.     MID$(hsdata$(6), 10, 6) = score$
  96.     MID$(hsdata$(6), 18, 8) = MID$(DATE$, 1, 6) + MID$(DATE$, 9, 2)
  97.     highscore$(6) = score$
  98.     FOR i = 1 TO 6
  99.         FOR j = 1 TO 6
  100.             IF i <> j THEN
  101.                 IF VAL(highscore$(i)) > VAL(highscore$(j)) THEN
  102.                     SWAP highscore$(i), highscore$(j)
  103.                     SWAP hsdata$(i), hsdata$(j)
  104.                 END IF
  105.             END IF
  106.         NEXT
  107.     NEXT
  108.     FOR i = 1 TO 5
  109.         MID$(hsdata$(i), 1, 2) = "0" + LTRIM$(STR$(i))
  110.     NEXT
  111.  
  112.     RETURN
  113.  

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Do I need to need a directory path for creating a file?
« Reply #7 on: October 27, 2021, 09:58:00 pm »
Binary, RA,... blah who wants to worry about fixed strings, it's only, what, 10 lines? 8 players according to OP.

These can be sorted as you load them from file, KISS ;-))

A sequential file you can edit if things go wrong.

44 LOC! https://www.qb64.org/forum/index.php?topic=1511.msg107586#msg107586

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Do I need to need a directory path for creating a file?
« Reply #8 on: October 27, 2021, 10:03:49 pm »
I say KIRAM it! (Keep it RA, Mark. )

KISS for programmers... (Keep it sequential, stupid.)

Pete 🤣
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Do I need to need a directory path for creating a file?
« Reply #9 on: October 27, 2021, 10:11:54 pm »
@Cobalt Love to see a binary example, if you have one.

Here's you a quick example which shows that RANDOM files and BINARY files are basically interchangeable:

Code: QB64: [Select]
  1. Type HighScore_Type
  2.     Name As String * 30
  3.     score As Long
  4.  
  5. Dim HighScores(10) As HighScore_Type
  6. Dim Whut(10) As HighScore_Type
  7.  
  8. HighScores(0).Name = "Pete"
  9. HighScores(1).Name = "Petr"
  10. HighScores(2).Name = "Steve"
  11. HighScores(3).Name = "Bplus"
  12. HighScores(4).Name = "Luke"
  13. HighScores(5).Name = "Felippe"
  14. HighScores(6).Name = "Richard"
  15. HighScores(7).Name = "Me"
  16. HighScores(8).Name = "You"
  17. HighScores(9).Name = "And"
  18. HighScores(10).Name = "Dog"
  19.  
  20. For i = 10 To 0 Step -1
  21.     total = total + Int(Rnd * 10000)
  22.     HighScores(i).score = total
  23.  
  24. Open "Temp.txt" For Random As #1 Len = Len(HighScores(0))
  25. For i = 0 To 10
  26.     Put #1, i + 1, HighScores(i) 'The Put is at position +1 here as we start at RECORD 1, not RECORD 0
  27.  
  28. Open "Temp.txt" For Binary As #1
  29. For i = 0 To 10
  30.     Get #1, i * Len(HighScores(0)) + 1, Whut(i) 'The Get is at byte +1 here, as we start getting at byte 1, not Byte 0
  31.  
  32. For i = 0 To 10
  33.     Print HighScores(i).Name; HighScores(i).score, Whut(i).Name; Whut(i).score

If you notice, I write to a RANDOM file, but then read that data back via BINARY access.

The only difference between the two is that RANDOM tracks your data by FILE NUMBER, while BINARY tracks the same data by BYTE POSITION.

If a Random file has records that are 100 bytes each in size, then the Binary file is going to be read at byte 1, 101, 201, 301, 401, 501 -- every 100 bytes, rather than at Record 1, 2, 3, 4, 5....

That's the only real difference in the two!
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Do I need to need a directory path for creating a file?
« Reply #10 on: October 27, 2021, 10:33:30 pm »
Got it.

So in a pure BINARY make to BINARY read method, we would have...

Code: QB64: [Select]
  1. TYPE HighScore_Type
  2.     Name AS STRING * 30
  3.     score AS LONG
  4.  
  5. DIM HighScores(10) AS HighScore_Type
  6. DIM Whut(10) AS HighScore_Type
  7.  
  8. HighScores(0).Name = "Pete"
  9. HighScores(1).Name = "Petr"
  10. HighScores(2).Name = "Steve"
  11. HighScores(3).Name = "Bplus"
  12. HighScores(4).Name = "Luke"
  13. HighScores(5).Name = "Felippe"
  14. HighScores(6).Name = "Richard"
  15. HighScores(7).Name = "Me"
  16. HighScores(8).Name = "You"
  17. HighScores(9).Name = "And"
  18. HighScores(10).Name = "Dog"
  19.  
  20. FOR i = 10 TO 0 STEP -1
  21.     total = total + INT(RND * 10000)
  22.     HighScores(i).score = total
  23.  
  24. OPEN "Temp.txt" FOR OUTPUT AS #1: CLOSE #1
  25.  
  26. OPEN "Temp.txt" FOR BINARY AS #1 'LEN = LEN(HighScores(0))
  27. FOR i = 0 TO 10
  28.     PUT #1, i * LEN(HighScores(0)) + 1, HighScores(i) 'The Put is at position +1 here as we start at RECORD 1, not RECORD 0
  29.  
  30. OPEN "Temp.txt" FOR BINARY AS #1
  31. FOR i = 0 TO 10
  32.     GET #1, i * LEN(HighScores(0)) + 1, Whut(i) 'The Get is at byte +1 here, as we start getting at byte 1, not Byte 0
  33.  
  34. FOR i = 0 TO 10
  35.     Print HighScores(i).Name; HighScores(i).score, Whut(i).Name; Whut(i).score
  36.  

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Donald Foster

  • Newbie
  • Posts: 63
    • View Profile
Re: Do I need to need a directory path for creating a file?
« Reply #11 on: October 28, 2021, 03:40:03 pm »
Thank you Everyone,

I used a sequential file along with _FileExits and it works great so far, but I far from done with the game.
I am very happy with the progress. Actually, it's been over 30 years since I created files.
By the way, bplus, none of game use image or sound files. All of my games are hand drawn using graphics statements.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Do I need to need a directory path for creating a file?
« Reply #12 on: October 28, 2021, 04:28:45 pm »
Quote
By the way, bplus, none of game use image or sound files. All of my games are hand drawn using graphics statements.

Yep, that's something to brag about IMHO. :)