Text Output To File

General FreeBASIC programming questions.
Post Reply
storm5510
Posts: 59
Joined: Nov 28, 2019 15:47

Text Output To File

Post by storm5510 »

I have a math program I wrote which produces a lot of text to be written to a file. While running, the file remains the size it was when I started. If I stop it, then all the text is written out in the file. I know a workaround. I wanted to ask to see if there was a simpler way to get it to write it out more frequently. Is there?
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Text Output To File

Post by jj2007 »

Open the file in append mode, and close it every now and then. It's also safer in case of crashes.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Text Output To File

Post by dodicat »

Here are some simple append and pre-pend routines.

Code: Select all


#Include "file.bi"
function savefile(filename As String,p As String) as string
    Dim As long n=freefile
    If Open (filename For Binary Access Write As #n)=0 Then
        Put #n,,p
        Close
    Else
        Print "Unable to save " + filename:sleep:end
    End If
    return filename
End function

Function loadfile(file as string) as String
	If FileExists(file)=0 Then Print file;" not found":Sleep:end
   dim as long  f=freefile
    Open file For Binary Access Read As #f
    Dim As String text
    If Lof(f) > 0 Then
      text = String(Lof(f), 0)
      Get #f, , text
    End If
    Close #f
    return text
end Function

function pre_pend(filename As String,txt As String) as string
    Dim As String s=loadfile(filename)
    If Len(s) Then savefile(filename,txt+s)
    return filename
End function

function ap_pend(filename As String,txt As String) as string
    Dim As String s=loadfile(filename)
    If Len(s) Then savefile(filename,s+txt)
    return filename
End function

dim as string file="Tester.txt"
savefile(file,!"a= pi*r^2\n") 'create the file (!"a= pi*r^2\n" = "a= pi*r^2"+chr(10))

print loadfile(file)
print
pre_pend(file,!"v=4/3*pi*r^3\n")
print loadfile(file)

print

ap_pend(file,!"Press a key . . .\n")
print loadfile(file)
sleep

dim as string g
for n as long=1 to 10000
    g+=str(sqr(n))+","
    if n mod 100=0 then ap_pend(file,g):g="":ap_pend(file,chr(10)+ "length = "+str(filelen(file))+chr(10))
next

ap_pend(file,chr(10))

ap_pend(file,!"End of test, remember to kill the file when done\n")
print loadfile(file)
print "File length = "; len(loadfile(file))

'kill file
print
sleep


 
storm5510
Posts: 59
Joined: Nov 28, 2019 15:47

Re: Text Output To File

Post by storm5510 »

Thank you for your replies. I did it like this:

Code: Select all

if count >= 10000 then
      print
      print "Emptying results file buffer..."
      print
      close #1
      sleep 2000
      count = 0
      open outfile for append as #1
end if
Every item written with Print #1 is counted and the variable "count" is incremented by one. It seems to work well enough. I thought about using an array of strings. Note my use of >=. I have learned, when doing something like this is to never pick a fixed target. If the specific target was jumped over, for some reason, then it would go on endlessly.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Text Output To File

Post by dodicat »

An easy method is simply to open a file at the program start, write to it, and close it at the program end.

Code: Select all


Open "primes.txt" For Output As #1
Print #1, "List of primes"

Function isprime(n As Ulongint) As Integer
    If (n=2) Or (n=3) Then Return -1
    If n Mod 2 = 0 Then Return 0
    If n Mod 3 = 0 Then Return 0
    Dim As Ulongint limit=Sqr(N)+1
    For I As Ulongint = 6 To limit Step 6
        If N Mod (i-1) = 0 Then Return 0
        If N Mod (i+1) = 0 Then Return 0
    Next I
    Return -1
End Function



Dim As String s
Dim As Long counter,primes
For n As Longint=50000*4294967295ull To 50000*4294967295ull+5000 
    counter+=1
    If isprime(n) Then 
        primes+=1
        s+=Str(n)+Chr(13,10)
        
        If counter > 500 Then 
            s=Rtrim(s,Chr(13,10))
            Print #1,s
            counter=0:s=""
            Print "Dumped to file ";primes;"  prime numbers"
            primes=0
        End If
        
    End If
Next n
Close #1

Print
Print "=========="
Shell "type primes.txt"
Kill "primes.txt"
Sleep

 
Maybe too simplistic for your requirements.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Text Output To File

Post by fxm »

From fbc 1.08.0, we can use the new FILEFLUSH keyword.
storm5510
Posts: 59
Joined: Nov 28, 2019 15:47

Re: Text Output To File

Post by storm5510 »

dodicat wrote:An easy method is simply to open a file at the program start, write to it, and close it at the program end.

Code: Select all


Open "primes.txt" For Output As #1
Print #1, "List of primes"

Function isprime(n As Ulongint) As Integer
    If (n=2) Or (n=3) Then Return -1
    If n Mod 2 = 0 Then Return 0
    If n Mod 3 = 0 Then Return 0
    Dim As Ulongint limit=Sqr(N)+1
    For I As Ulongint = 6 To limit Step 6
        If N Mod (i-1) = 0 Then Return 0
        If N Mod (i+1) = 0 Then Return 0
    Next I
    Return -1
End Function



Dim As String s
Dim As Long counter,primes
For n As Longint=50000*4294967295ull To 50000*4294967295ull+5000 
    counter+=1
    If isprime(n) Then 
        primes+=1
        s+=Str(n)+Chr(13,10)
        
        If counter > 500 Then 
            s=Rtrim(s,Chr(13,10))
            Print #1,s
            counter=0:s=""
            Print "Dumped to file ";primes;"  prime numbers"
            primes=0
        End If
        
    End If
Next n
Close #1

Print
Print "=========="
Shell "type primes.txt"
Kill "primes.txt"
Sleep

 
Maybe too simplistic for your requirements.
It did not work for me this way. It would hold on to megabytes of data spanning hours of running. I had to find an alternate method.
fxm wrote:From fbc 1.08.0, we can use the new FILEFLUSH keyword.
I checked the help file. This was not listed. Actually, I do not know what version I am using. It has a 9/27/2019 date stamp.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Text Output To File

Post by fxm »

It may be fbc version 1.07.1:

Code: Select all

Print __FB_SIGNATURE__
Sleep
Version 1.08.0 is currently a working version for the next upcoming official revision.
It can be found at: http://users.freebasic-portal.de/stw/builds/
mmbcastle
Posts: 16
Joined: May 05, 2021 11:46
Location: Tennessee, USA
Contact:

Re: Text Output To File

Post by mmbcastle »

It did not work for me this way. It would hold on to megabytes of data spanning hours of running. I had to find an alternate method.

fxm wrote:
From fbc 1.08.0, we can use the new FILEFLUSH keyword.


I checked the help file. This was not listed. Actually, I do not know what version I am using. It has a 9/27/2019 date stamp.
I understand that different people are probably working on the wiki vs development, etc. I found FILEFLUSH in the online manual and I was trying to use it to replace the DOS Int21 stuff I was doing in an old library. I dug around for a long time before I realized that command wasn't actually in my recently downloaded version of FB (1.07.2, Dec 25, 2020). It would be really handy if the docs included a "version added" section or something like that. Perhaps just in the "Differences from QB" section it could say "New to FreeBASIC starting in v1.08.x".
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Text Output To File

Post by fxm »

I set myself this rule on the wiki (which could be re-discussed in the Documentation Forum):
- The documentation on wiki is updated daily if possible (after each change without waiting for the release of a new fbc version) so that users of the current (unofficial) version have reference documentation.
- For already existing keywords, any recent behavior change is reported in the description with the fbc version number for which it will be or was recently applied.
- For new keywords, nothing is reported because it is obvious to realize that the fbc version that one is using does not support them.

On the other hand, with each official fbc version release, a matching downloadable documentation is associated with it (from the wiki version sampled at this time).
mmbcastle
Posts: 16
Joined: May 05, 2021 11:46
Location: Tennessee, USA
Contact:

Re: Text Output To File

Post by mmbcastle »

Don't get me wrong, I love the wiki and all the FB docs and support. It's been invaluable for me as I enter into a massive QB -> FB conversion. From what little I know about it I also already look forward to 1.08 and I'm considering jumping ahead to it, so I appreciate that the docs are keeping up with development.
- For new keywords, nothing is reported because it is obvious to realize that the fbc version that one is using does not support them.
For someone like me that's entering into this mid-stream and learning the differences and conventions as I go, this particular case wasn't obvious. I spent a bit of time assuming I was getting the fileflush syntax wrong or somehow messing up the #include for file.bi. I dug around in file.bi to see what the declare looked like (obviously not finding it). Someone had mentioned earlier that the calling conventions were different between #lang "fb" and "fblite" so I was wondering if that was the problem. In the end, I pretty much stumbled on a post stating that fileflush was a new feature in 1.08. Otherwise, I would either still be chasing it or assumed it was a documentation error.

I certainly don't expect you to change things for me. I just thought my experience might help others in a similar position.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Text Output To File

Post by MrSwiss »

The 2 new additions in "file.bi" are defined as follows (FBC 1.08, current-dev):

Code: Select all

declare function FileFlush alias "fb_FileFlush" ( byval filenumber as long = -1, byval systembuffer as long = 0 ) as long
declare function FileSetEof alias "fb_FileSetEof" ( byval filenumber as long ) as long
NOTE: not yet in any "official release" version.
SamL
Posts: 58
Joined: Nov 23, 2019 17:30
Location: Minnesota

Re: Text Output To File

Post by SamL »

Nice new FILEFLUSH keyword !!!
Post Reply