Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 16:57 11 Nov 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 : Line Input versus Input$

Author Message
panky

Guru

Joined: 02/10/2012
Location: Australia
Posts: 1116
Posted: 07:11am 16 Jul 2018
Copy link to clipboard 
Print this post

According to the manual,

LINE INPUT [promp$,]string-variable
is targetted at reading a complete line from console or

LINE INPUT #nbr,string-variable
.. from a serial comms port opened previously opened for INPUT.


INPUT$(nbr,[#]fnbr)
is targeted at reading nbr numbers of characters also from a serial comms port previously opened for INPUT. If nbr is greater than the number of characters available, the entire buffer will be read ( or file - see below).

Both forms also work if a file is opened for INPUT.

There is one caveat!!!

If the file being read from has NO CR LF at the end, then LINE INPUT will not read the last character but INPUT$ will. See example code below.

If the file includes a CR LF at the end, then both forms of input work the same.

panky

  Quote  
DIM tstdata$ = "abc"
DIM junk1$,junk2$
PRINT "Test 1"
OPEN "test1.dat" FOR OUTPUT AS #1
PRINT "First, print data as it is written to file"
' write characters abc to file with no trailing CR LF
PRINT #1, tstdata$;
PRINT tstdata$;
PRINT
CLOSE #1

PRINT
OPEN "test1.dat" FOR INPUT AS #1
LINE INPUT #1, junk1$
PRINT "File input using LINE INPUT ",junk1$
CLOSE #1

OPEN "test1.dat" FOR INPUT AS #1
junk2$ =
INPUT$(255, #1)
PRINT "File input using INPUT$ ",junk2$

CLOSE #1
END


... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it!
 
flip
Senior Member

Joined: 18/07/2016
Location: Australia
Posts: 117
Posted: 01:30pm 16 Jul 2018
Copy link to clipboard 
Print this post

I'm not seeing that behaviour. When I tried above, I got as follows:
DOS MMBasic Ver 5.04.08
Copyright 2011-2017 Geoff Graham

Test 1
First, print data as it is written to file
abc

File input using LINE INPUT abc
File input using INPUT$ abc
>

The file is 3 bytes long (No CRLF) but can't see any problems (interesting and handy that INPUT$(255) doesn't complaint even though it's a 3-byte file)

Regards,
PhilEdited by flip 2018-07-17
 
panky

Guru

Joined: 02/10/2012
Location: Australia
Posts: 1116
Posted: 01:40am 17 Jul 2018
Copy link to clipboard 
Print this post

Hi Phil,

You are absolutely correct - in MMBasic for DOS ver 5.04.08 it works correctly (that is, LINE INPUT picks up all characters in the file even without a trailing CR).

But, in MMBasic Plus ver 5.04.10b3, LINE INPUT skips the last character in a file if there is no trailing CR.

I think what is happening is that LINE INPUT, when used with the console as input, gets characters until it receives a CR (the Enter key was pressed), it discards this character (the CR) and terminates the LINE INPUT command with the correct characters in the variable assigned.

When used with a file as the input source, LINE INPUT gets to the end of the file and as before, discards the last character because it thinks it is the terminating character as the CR was above. The terminating trigger was in fact an internal flag indicating end-of-file so the last character has been incorrectly discarded.

One for Geoff and/or Peter to comment on I think, but I am pretty sure it is not intended to operate this way (ie. a bug!).

Doug.


... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it!
 
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