Line Input #n produces nullpointers

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Line Input #n produces nullpointers

Post by jj2007 »

I stumbled over this by accident. The help file doesn't mention this potentially problematic behaviour.

Code: Select all

#include "file.bi"	' for FileExists()

Function Recall(fname As String, locArray() As String) As Integer
  Dim As Integer ct=0, cursize=100	' locArray is a local representation of a dynamic array
  If Open(fname For Input As #1) = 0 Then
	Do While Not Eof(1)
		if ct=0 or ct>cursize then
			cursize+=cursize shr 1
			ReDim Preserve locArray(cursize)
		endif
		Line Input #1, locArray(ct)
		ct+=1
	Loop
	Close #1
	ReDim Preserve locArray(ct)
  Else
	Print "Error opening file"
  End If
  Return ct
End Function

Dim MyArray() As string
if FileExists("test.tmp")=0 then
	Open "test.tmp" for output as #1
	For i As Integer=0 To 9
		if i<>5 then
			Print #1, "This is string #";i
		else
			Print #1, ""
		endif
	Next
	Close #1
	Print "Testfile created"
endif

if Recall("test.tmp", MyArray()) then
  Print "records loaded:";ubound(MyArray)
  For i As Integer=0 To ubound(MyArray)-1		' print the first and last Cells
	if StrPtr(MyArray(i))=0 then
		print "### nullpointer in line";i;" ###"
	endif
	Print i, MyArray(i)
  Next
  Print "OK"
endif
Sleep
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Line Input #n produces nullpointers

Post by fxm »

An empty string induces a null 'StrPtr' (no associated character data).
By reading an empty line in a text file (the line 5 in your example), 'Line Input #' returns an empty string.
Last edited by fxm on Oct 14, 2018 12:32, edited 1 time in total.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Line Input #n produces nullpointers

Post by jj2007 »

Yes, that is what I found out with trial & error.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Line Input #n produces nullpointers

Post by counting_pine »

Since StrPtr returns a pointer (by definition), it's worth considering the possibility that it will return a Null pointer.
KeyPgOpStrptr is explicit about this possibility:
In case of empty string (only for variable length strings), Operator Strptr returns a null pointer.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Line Input #n produces nullpointers

Post by dodicat »

HELP FILE;
Reads a line from an open text file (opened for Input through a bound file number) and stores it in a string variable.

A line of text ends at, but does not include the end of line characters. An end of line character may be the LF character (Chr(10)) or the CRLF character pair (Chr(13,10)).
'--------
line input ignores the end character produced by print #1,~~
So you should replace this end character at each line input
(your recall)

Code: Select all

 Function Recall(fname As String, locArray() As String) As Integer
  Dim As Integer ct=0, cursize=100   ' locArray is a local representation of a dynamic array
  dim as string s
  If Open(fname For Input As #1) = 0 Then
   Do While Not Eof(1)
      if ct=0 or ct>cursize then
         cursize+=cursize shr 1
         ReDim Preserve locArray(cursize)
      end if
      Line Input #1, s:locArray(ct)=s+chr(10)
      ct+=1
   Loop
   Close #1
   ReDim Preserve locArray(ct)
  Else
   Print "Error opening file"
  End If
  Return ct
End Function 
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Line Input #n produces nullpointers

Post by jj2007 »

counting_pine wrote:KeyPgOpStrptr is explicit about this possibility:
In case of empty string (only for variable length strings), Operator Strptr returns a null pointer.
Good. Maybe it should also be added to the help file, which n00bs are more likely to find and consult.
dodicat wrote:So you should replace this end character at each line input
...
Line Input #1, s:locArray(ct)=s+chr(10)
I doubt that many applications would appreciate a linefeed at the end of a string.
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Line Input #n produces nullpointers

Post by fxm »

jj2007 wrote:Maybe it should also be added to the help file, which n00bs are more likely to find and consult
What is the "help file"?
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Line Input #n produces nullpointers

Post by counting_pine »

The official help file was last generated from the wiki with the last release of FreeBASIC, which is a couple of years ago. Unfortunately that line wasn't in it then it was produced, but in any future versions it will be there. The wiki is the best place to go for up-to-date information.
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Line Input #n produces nullpointers

Post by fxm »

A current build ('freebasic_manual.chm') is available at http://users.freebasic-portal.de/stw/builds/.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Line Input #n produces nullpointers

Post by dodicat »

jj2007 wrote:
counting_pine wrote:KeyPgOpStrptr is explicit about this possibility:
In case of empty string (only for variable length strings), Operator Strptr returns a null pointer.
Good. Maybe it should also be added to the help file, which n00bs are more likely to find and consult.
dodicat wrote:So you should replace this end character at each line input
...
Line Input #1, s:locArray(ct)=s+chr(10)
I doubt that many applications would appreciate a linefeed at the end of a string.
Indeed jj2007
Line input corrupts the file coming in, so you repair it on the fly.
Agreed, not good, better to load a file into a string with get.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Line Input #n produces nullpointers

Post by jj2007 »

I have posted a new Recall(filename, stringarray) version, blazing fast and probably portable, in the spreadsheet thread. Its Cell(row, column, stringarray) function takes account of the nullpointer problem and returns valid pointers.

Btw often a nullpointer is not a problem, many APIs are tolerant in this respect. But once in a while you might end up with "unexplainable" crashes, so it's good to point the user to this behaviour.
Post Reply