Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 03:18 24 May 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Can't read multiple variables with INPUT #

Author Message
PeteCotton

Guru

Joined: 13/08/2020
Location: Canada
Posts: 529
Posted: 06:14am 16 Feb 2025
Copy link to clipboard 
Print this post

I'm being a bit thick - and this is almost certainly operator error, but I was trying to write out multiple integer values to a file and read them back in.

If I'm reading the manual right, I should be able to do
INPUT #1,a,b
to read values into a and b



But when I run the code below, it writes the variables out okay to the file. But when I do the reading back in, it loads a but not b
' This bit works - the file contains 1,2
DIM INTEGER a=1, b=2
OPEN "test.dat" FOR OUTPUT AS #1
PRINT #1,a,b
CLOSE #1
a=0
b=0
' This is where I get confused
OPEN "test.dat" FOR INPUT AS #1
INPUT #1,a,b
CLOSE #1
PRINT a
PRINT b

Results in an output of a=1 and b=0

What am I doing wrong?

I should note - that this is just for my own curiosity, I saved the data one variable at a time to each newline and that worked absolutely fine - I'm just trying to understand how to do it for the future.

Thanks.
 
PeteCotton

Guru

Joined: 13/08/2020
Location: Canada
Posts: 529
Posted: 06:20am 16 Feb 2025
Copy link to clipboard 
Print this post

Well, don't I feel a bit foolish.

I re-read an old BASIC page and they used this line:
PRINT #1,a;",";b
to write out the data. An I just tested it and it worked fine.

Sorry for the stupid question (it is quite late here - that's my excuse and I'm sticking to it   )
 
Peter63
Regular Member

Joined: 28/07/2017
Location: Sweden
Posts: 47
Posted: 06:42am 16 Feb 2025
Copy link to clipboard 
Print this post

You found a solution to this yourself   . I was just wondering if you checked how the variables were stored in the file the first version you used?

/Peter63
 
PeteCotton

Guru

Joined: 13/08/2020
Location: Canada
Posts: 529
Posted: 06:53am 16 Feb 2025
Copy link to clipboard 
Print this post

  Peter63 said  You found a solution to this yourself   . I was just wondering if you checked how the variables were stored in the file the first version you used?

/Peter63


Yes.
PRINT #1,a;",";b
Stored it as
1, 2

PRINT #1,a,b
Stored it as
1   2

Cheers,

Pete
 
Peter63
Regular Member

Joined: 28/07/2017
Location: Sweden
Posts: 47
Posted: 07:02am 16 Feb 2025
Copy link to clipboard 
Print this post

OK, thank you   . I just got a little curious about this. As EEVBlog David says "trap for newcomers" or "young players"

/Peter63
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2458
Posted: 07:33am 16 Feb 2025
Copy link to clipboard 
Print this post

There was a recent demo. of that in formatting csv files  thread.
 
PeteCotton

Guru

Joined: 13/08/2020
Location: Canada
Posts: 529
Posted: 05:26pm 16 Feb 2025
Copy link to clipboard 
Print this post

  phil99 said  There was a recent demo. of that in formatting csv files  thread.


Doh! I read that thread as well, but it just didn't register with me  
 
PeteCotton

Guru

Joined: 13/08/2020
Location: Canada
Posts: 529
Posted: 05:43pm 16 Feb 2025
Copy link to clipboard 
Print this post

  Peter63 said  OK, thank you   . I just got a little curious about this. As EEVBlog David says "trap for newcomers" or "young players"

/Peter63

Yes, I think my problem is that I misremember things about BASIC, and then expect things to work one way - and when they don't, I get confused (e.g. my other thread thinking that BASIC arrays stared at Base 1).

It's about retraining my brain. I had a problem the other day which tripped me up - which was I was using "!=" instead of "<>" for not equals. But my brain reads both as "not equals", so I couldn't see what was wrong with the syntax.

Still - even working through these sorts of issues is a lot of fun. There's that sense of discovery I had back in my youth. Modern languages do everything so easily and so quickly, that it's rare that you have to optimise anything. With BASIC it feels a lot more "old school".

Case in point. I'm going to take my new found knowledge about how to save multiple variables at a time, and see if it shaves some millseconds off the save/load speed of my game.
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 396
Posted: 09:48pm 16 Feb 2025
Copy link to clipboard 
Print this post

  PeteCotton said  
Yes, I think my problem is that I misremember things about BASIC, and then expect things to work one way - and when they don't, I get confused (e.g. my other thread thinking that BASIC arrays stared at Base 1).


Some BASICs do start arrays at one. In fact the ANSI/ISO standard for BASIC is one of those. It supplies the OPTION BASE command for changing to 0 based indexing but it really isn't needed since you can define the dimensions in a DIM statement:

DIM A(0 TO 5) will give you 6 elements no matter what OPTION BASE is set to

DIM A(101 TO 110) starts at 101 and provides 10 elements (of course!)

This makes it great for keeping track of yearly figures:

DIM PROFIT(1947 TO 2025) can store info on years 1947 to 2025 and if you want the profit for the year 1963, for example, you can just retrieve PROFIT(1963) instead of having to figure out which element stores that.
Edited 2025-02-17 07:49 by toml_12953
 
PeteCotton

Guru

Joined: 13/08/2020
Location: Canada
Posts: 529
Posted: 10:00pm 16 Feb 2025
Copy link to clipboard 
Print this post

  toml_12953 said  ... but it really isn't needed since you can define the dimensions in a DIM statement:
....
DIM A(101 TO 110) starts at 101 and provides 10 elements (of course!)
I didn't know that. Very interesting. Thanks!
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2458
Posted: 10:46pm 16 Feb 2025
Copy link to clipboard 
Print this post

Not as convenient as DIM PROFIT(1947 TO 2025) but but not too bad.

Use element 0 to store the offset and treat the rest of the array as if it were base 1.

> Dim PROFIT(2025 - 1947 + 1)
> PROFIT(0) = 1947 - 1
> ? PROFIT(0)
1946
> for n=1 to (2025 - 1947 + 1) :PROFIT(n)=n+100 :next
> ? PROFIT(1947-PROFIT(0))
101
> ? PROFIT(2024-PROFIT(0))
178
> ? PROFIT(2025-PROFIT(0))
179
>
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025