Putting null string to file using crt routines puts or fputs

New to FreeBASIC? Post your questions here.
robvanvliet
Posts: 2
Joined: Aug 08, 2013 7:20

Putting null string to file using crt routines puts or fputs

Postby robvanvliet » Jul 25, 2014 10:49

[commit][/commit]Soory, I've no experience using forums, so might do things clumsy. As I'm blind
I don't see much of the instructions.

I use 'fputs' to write my strings to file. A nonzero return of fputs indicates error.
Fputs requires a zstring ptr as input parameter, so I use strptr to pass my strings.
However, using strptr on an empty string yields 0, causing the error return of fputs.

Here is a code example using puts (works similar):

#include once "crt.bi"

sub try_puts(s as string)
dim sp as zstring ptr
sp = strptr(s)
if puts(sp) <> 0 then
print "error puts called with pointer: ";str(sp)
end if
end sub

try_puts("jan")
try_puts("")
sleep
codeFoil
Posts: 255
Joined: Dec 22, 2011 4:45
Location: United States
Contact:

Re: Putting null string to file using crt routines puts or f

Postby codeFoil » Jul 26, 2014 7:07

Welcome to the forum.

I am not quite sure what your question is, but I can try to explain the results you have mentioned.

Since an empty string has no contents, no memory is allocated for it, so any time you need to pass the address of a STRING that might be empty, you need to check for that special case.

In this instance, you can simply prevent the call altogether because attempting to write zero bytes to a file is tantamount to not writing at all.

Code: Select all

#INCLUDE once "crt.bi"

SUB TryPuts(BYREF s as const STRING)

    IF Len(s) ANDALSO puts(StrPtr(s)) THEN
        Print "puts failed."
    END IF

END SUB

TryPuts "jan"
TryPuts ""

Sleep



In the above code, you would not normally expect to see the error message.
The check is accomplished by using the ANDALSO operator. If the length of the STRING is zero, the second operate in the expression will not be evaluated. If the length is not zero, the value of the expression will depend on the return value of the puts function.
codeFoil
Posts: 255
Joined: Dec 22, 2011 4:45
Location: United States
Contact:

Re: Putting null string to file using crt routines puts or f

Postby codeFoil » Jul 26, 2014 7:29

Here is an alternative that relies only on the return of StrPtr

Code: Select all

SUB TryPuts(BYREF s as const STRING)

    VAR sPtr = StrPtr(s)
    IF sPtr ANDALSO puts(sPtr)  THEN
        Print "puts failed."
    END IF

END SUB
fxm
Posts: 9949
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Putting null string to file using crt routines puts or f

Postby fxm » Jul 26, 2014 8:48

Corrected codes:

Code: Select all

#INCLUDE once "crt.bi"

SUB TryPuts(BYREF s as const STRING)

    #print typeof(StrPtr(s))
    IF Len(s)=0 Orelse puts(StrPtr(s)) THEN
        Print "puts failed."
    END IF

END SUB

TryPuts "jan"
TryPuts ""

Sleep

Code: Select all

#INCLUDE once "crt.bi"

SUB TryPuts(BYREF s as const STRING)
 
    VAR sPtr = StrPtr(s)
    #print typeof(sPtr)
    IF sPtr=0 ORELSE puts(sPtr)  THEN
        Print "puts failed."
    END IF

END SUB

TryPuts "jan"
TryPuts ""

SLEEP


Remark: Any string type argument may be directly passed to a procedure referring to a parameter declared as 'zstring ptr'. The compiler performs itself an automatic conversion (without warning message) between string and 'zstring ptr'.
So we can use directly 'puts(s)'.
Last edited by fxm on Jul 26, 2014 9:05, edited 1 time in total.
fxm
Posts: 9949
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Putting null string to file using crt routines puts or f

Postby fxm » Jul 26, 2014 8:57

@dkl:

The two above codes work no more with FBC 0.91.0:
error 180: Invalid assignment/conversion, at parameter 1 of PUTS()
because 'typeof(StrPtr(s)) = CONST ZSTRING * 4 CONST PTR' (with 's' being a Const String).
Is this normal?

Remark (with 's' being a Const String):
- 'puts(Cast(Zstring [Const] Ptr, StrPtr(s)))' works
- No problem with 'puts(s)', because of automatic conversion?
dkl
Site Admin
Posts: 3212
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Putting null string to file using crt routines puts or f

Postby dkl » Jul 26, 2014 13:30

If you want to use FB dynamic Strings with puts, then you can check Len() = 0 or Strptr() = 0 to detect empty strings and give them special treatment:

Code: Select all

#include once "crt.bi"

sub try_puts(byref s as string)
   var p = strptr(s)
   if p then
      puts(p)
   else
      puts("")
   end if
end sub

try_puts("jan")
try_puts("")
sleep


---

Regarding Const:

Yea, in the development version of FB, if you have a Const String, then StrPtr() will also return a Const Zstring Ptr.

The problem is that FB's C runtime headers do currently not use Const correctly, for example:

Code: Select all

'' puts is declared as:
declare function puts (byval as zstring ptr) as long
'' but it should be:
declare function puts (byval as const zstring ptr) as long


and FB refuses to pass Const strings to the non-Const parameters.
fxm
Posts: 9949
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Putting null string to file using crt routines puts or f

Postby fxm » Jul 26, 2014 14:10

@dkl:

Why this works with 'puts(s)' (with 's' being a Const String)?

Code: Select all

#include once "crt.bi"

Dim As Const String s = "Hello, world!"

'puts(Strptr(s))  '' error 180: Invalid assignment/conversion, at parameter 1 of PUTS()
puts(s)  '' ok

Sleep

What does the automatic conversion of FB:
Const String => Const Zstring Ptr?
dkl
Site Admin
Posts: 3212
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Putting null string to file using crt routines puts or f

Postby dkl » Jul 26, 2014 15:14

It must be a bug; ticket opened: #734

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 2 guests