QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: JohnUKresults on May 14, 2021, 12:31:19 pm

Title: Is there some magic about the number 1?
Post by: JohnUKresults on May 14, 2021, 12:31:19 pm
Hi folks

I have a short piece of code within a large program which is driving me mad!! It works fine if I run it as a stand alone bit of code but not when I have it within the program.

The object of the code is to set up some csv files with fixed data fields, the number and names of the files are "lap" plus the lap number. When I run as the separate little program, it correctly installs lap1, lap2 etc. but when I am running it within the main program, even though the inkeys pause shows the correct file name of lap1 having been strung together, it never opens a lap1 file. Frustratingly, if I set the loop to start at 0 it will open a lap 0, then skip lap1 and go to lap 2 - all subsequent lap files open just fine.

I can't see where it's going wrong. Why should it skip file 1 in the main program?

Ignore all the While inkey$="":wend bits - I just put those in so I could see the variables and file names.

Here are the code snippets - the first is the stand-alone version, modified slightly as it's not using a file name string or looking up in a separate random access file:
Code: QB64: [Select]
  1. REM *** create lap files ***
  2.  
  3. FOR x% = 1 TO 5
  4.     x$ = _TRIM$(STR$(x%))
  5.     PRINT x%, x$: WHILE INKEY$ = "": WEND
  6.     lapfile$ = "c:\race\rrclaps\test2\" + "lap" + x$ + ".csv"
  7.     PRINT lapfile$: WHILE INKEY$ = "": WEND
  8.     filenum% = 10 + x%
  9.     OPEN lapfile$ FOR OUTPUT AS filenum%
  10.     FOR y% = 1 TO 10
  11.         WRITE #filenum%, _TRIM$(STR$(y%)), "00:00:00.000", "00:00:00.000", "00:00:00.000"
  12.     NEXT y%
  13.  
  14. NEXT x%
  15.  
  16.  

This is the same code but within the main program, fil$ being the current file name and laps% being the number of laps to be run. File 5 stores a list of race numbers, test2 is the filename for testing, so it's hard-coded into the short program for clarity, but uses fhe fil$ name to point to the right folder in the main program:
Code: QB64: [Select]
  1.  
  2.             REM *** create lap files ***
  3.             FOR x% = 1 TO laps%
  4.                 x$ = _TRIM$(STR$(x%))
  5.                 PRINT x%, x$: WHILE INKEY$ = "": WEND
  6.                 lapfile$ = "c:\race\rrclaps\" + fil$ + "\" + "lap" + x$ + ".csv"
  7.                 PRINT lapfile$: WHILE INKEY$ = "": WEND
  8.                 filenum% = 10 + x%
  9.                 OPEN lapfile$ FOR OUTPUT AS filenum%
  10.                 FOR y% = 1 TO loaded%
  11.                     GET 5, y%
  12.                     WRITE #filenum%, _TRIM$(num$(1)), "00:00:00.000", "00:00:00.000", "00:00:00.000"
  13.                 NEXT y%
  14.             NEXT x%
  15.  

Anyone got any thoughts?
Title: Re: Is there some magic about the number 1?
Post by: justsomeguy on May 14, 2021, 12:40:46 pm
You might try using 'filenum% = FREEFILE' instead of 'filenum% = 10 + x%' on line 8. Just in case you are using that file number somewhere else in your program.
Title: Re: Is there some magic about the number 1?
Post by: JohnUKresults on May 14, 2021, 12:46:53 pm
That's a plan!!

I'll try it with filenum%=20+x% first.

Don't want to use FREEFILE as I need to fix in my mind which filenumbers will be used for when I have to be looking through each of the lap files.

It's quite possible that 11 is already being used - hadn't thought of that.

Cheers
Title: Re: Is there some magic about the number 1?
Post by: JohnUKresults on May 14, 2021, 12:49:23 pm
Hi. That's nailed it. Knew it would be something stupid!!

Thanks for your pointer. :-)
Title: Re: Is there some magic about the number 1?
Post by: bplus on May 14, 2021, 01:30:44 pm
Number 1 is special because it's the beginning of all the other, then it's the multiplicative identity and plenty of other special properties, even in our culture, most prefer to be number 1.

For file numbers, maybe not so much :)
Title: Re: Is there some magic about the number 1?
Post by: doppler on May 14, 2021, 07:00:31 pm
Number one is easier and faster than number two.
Title: Re: Is there some magic about the number 1?
Post by: Fifi on May 19, 2021, 08:11:57 am
Hi doppler,

Number one is easier and faster than number two.

In general computing (loop, table, array, etc.) is'nt the Number One element 0? ;)

BTW, did you test the beta of my install script I made for you on your Raspberry Pi?

Looking forward to know if it's working.
Title: Re: Is there some magic about the number 1?
Post by: bplus on May 19, 2021, 11:02:25 am
Number one is easier and faster than number two.

Fifi has a point when it comes to easier and faster doing number 0 beats them all! :O
Title: Re: Is there some magic about the number 1?
Post by: doppler on May 20, 2021, 09:53:05 am
@Fifi @bplus Did my rude joke over your heads ?  Think really crude. No math involved.

fifi, will play this week-end. trying it out.
Title: Re: Is there some magic about the number 1?
Post by: SMcNeill on May 20, 2021, 12:32:06 pm
@Fifi @bplus Did my rude joke over your heads ?  Think really crude.

Quote
Number one is easier and faster than number two.

This is true, but neither is as fun and enjoyable as a sixty-nine.
Title: Re: Is there some magic about the number 1?
Post by: Richard Frost on May 21, 2021, 03:42:19 am
One is the loneliest number that you'll ever do
Two can be as bad as one
It's the loneliest number since the number one, ah.

 - Three Dog Night

p.s. Number 0 requires a cork, maybe two of them.