November 28, 2020, 10:25:15 AM

Author Topic: [bb] PrintF() function by Dan [ 1+ years ago ]  (Read 485 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] PrintF() function by Dan [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : PrintF() function
Author : Dan
Posted : 1+ years ago

Description : Prints a text to a file. If the file exists, it adds the text to the end of the file.
Every PrintF() call adds Chr$(13)+Chr$(10) to the end of the line (cr/lf).
The file is then openable in the editor (provided that you have used the printable chars ...)

The usage is Simple:

Code: [Select]
Type writeout
Field Filename$
Field OldFilename$
Field filestreamID
Field open
Field filenameset
End Type
Global pfile.writeout = New writeout

PrintF("c:filename00.txt",1) ; to set the filename
PrintF("text like you would use in a Print command") ;Writes to the same file
PrintF("this is then the new line in the file")      ;Writes to the same file and adds a new line
;...
PrintF("")   ; The last line of the text, equals the CloseFile command

PrintF("and this")                                   ;Writes to the same file and adds a new line

;comment the next line to remove the RunTimeError
PrintF("c:filename01.txt",1)  ;Should cause an RunTimeError because the last command was not  PrintF("")

PrintF("")     ;close the last file (in this case c:filename00.txt)
PrintF("c:filename01.txt",1)  ;Comment The Previous PrintF("c:filename01.txt",1) line and this line will open a new file !
PrintF("Writes to the filename01.txt")
PrintF("")    ;Closes the filename01.txt


After commenting the "PrintF("c:filename01.txt",1) ;Should cause an RunTimeError because the last command was not  PrintF("")" you should find 2 new files in your C:directory
(if you have the sufficient rights to write to it) [/i]

Code :
Code: BlitzBasic
  1. ;====================================================================
  2. ; Project: PrintF function
  3. ; Version: 1.0
  4. ; Author: Dan
  5. ; Email: -.-
  6. ; Copyright:  PD
  7. ; Description:      
  8. ;     Prints Text To a file ! (easy way)
  9. ;     every time PrintF is called it writes the text to the end of the last file set with PrintF("c:hey.txt,1) file.
  10. ;     Basic error checking is implemented,but you will need to ensure that the filename is valid
  11. ;     every PrintF("text") call adds CrLf ($0d and $0a bytes to the end of the end of the txt$)
  12. ;     so that the file can be opened with notepad
  13. ; Usage:
  14. ;       PrintF(Filename,1) to set the filename
  15. ;       PrintF("text")     to write to the file above
  16. ;       PrintF("")         to close the opened file !!!
  17. ;                          so that a new filename can be set
  18. ;===============================================================================================
  19.  
  20.  
  21. Function PrintF(Txt$="",setf=0)
  22. ;Copy next 8 lines to the beginning of your program, and uncomment them (remove ; )
  23. ;       Type writeout
  24. ;               Field Filename$
  25. ;               Field OldFilename$
  26. ;               Field filestreamID
  27. ;               Field open
  28. ;               Field filenameset
  29. ;       End Type
  30. ;       Global pfile.writeout = New writeout
  31. ;       Const loaddebug=0                                               ;Used for CheckLoad + CheckAnimLoad functions
  32. ;       If loaddebug=1 Then PrintF("R:filesize.txt",1)                  ;to make a list with filenames and filesize. useful for releasing the games, to check if the file has been modified (is it only a simple size check)
  33. ;usage:
  34. ;PrintF("r:     est.txt",1)     ;to set the filename
  35. ;PrintF("test text")            ;to write a line of text to the file
  36. ;PrintF("")                                     ;To close the file ! Important before using another file to write!
  37. ;                                                       ;else it writes to the same file again
  38.         Select True
  39.     Case setf=0 And pfilefilenameset=1
  40.                 If Len(Txt$)>0                                          ;Is Length of the Text$ greater than 0
  41.                         If pfileopen=0                                  ;has the file allready been opened ?
  42.                                 If pfileOldFilename$="" Then pfileOldFilename$=pfileFilename$   ;if no, set the oldfilename as filename$
  43.                                
  44.                                 If FileType(pfileFilename$)=0                   ;Doesnt Exists, create new one
  45.                                         pfilefilestreamID=WriteFile(pfileFilename$)
  46.                                 ElseIf FileType(pfileFilename$)=2                       ;It is a directory, Stop the program
  47.                                         RuntimeError "PrintF: The Filename is a directory, please check your script"
  48.                                 ElseIf FileType(pfileFilename$)=1                       ;File Exists, open it to make additions !
  49.                                         pfilefilestreamID=OpenFile(pfileFilename$)
  50.                                 EndIf
  51.                                 If pfilefilestreamID=0                                          ;Check if the file could be opened
  52.                                         RuntimeError "PrintF: error cannot open "+pfileFilename$
  53.                                 Else                                                                            ;The file exists, set the writing position to the end of the file !
  54.                                         SeekFile (pfilefilestreamID,FileSize(pfileFilename$))
  55.                                 EndIf
  56.                                 pfileopen=1                                                             ;Global flag to indicate that the file is open !
  57.                         ElseIf pfileopen=1                                                              ;File has allready been opened, check if the filename is same (to prevent writing data to a wrong file !)
  58.                                 If pfileFilename$<>pfileOldFilename$
  59.                                         If pfilefilestreamID>0 Then CloseFile pfilefilestreamID
  60.                                         RuntimeError ("PrintF: Filename Missmatch "+pfileFilename$+" is not "+pfileOldFilename$)
  61.                                 EndIf
  62.                         EndIf
  63.                 ;Write text string into the FilestreamId
  64.                         For x=1 To Len(Txt$)
  65.                                 WriteByte pfilefilestreamID,Asc(Mid$(Txt$,x,1))
  66.                         Next
  67.                 ;And add cr+lf, so it can be readed in text editor as new line
  68.                         WriteByte pfilefilestreamID,$0d
  69.                         WriteByte pfilefilestreamID,$0a
  70.                 Else                                                                    ;if length of the text$ is 0 then the file should be closed !
  71.                         If pfilefilestreamID>0 Then CloseFile pfilefilestreamID
  72.                         pfileopen=0
  73.                         pfileOldFilename=""
  74.                 EndIf
  75.         Case setf=1 And pfilefilenameset=0
  76.            If Txt$="" Then RuntimeError "PrintF (txt$,1) is used To set a filename, And txt$ cannot be empty!"
  77.                 pfileFilename$=Txt$
  78.                 pfilefilenameset=1
  79.         Case setf=0 And pfilefilenameset=0
  80.             RuntimeError "The Filename was not been set, use PrintF(''c:Filename'',1) before calling PrintF(''text'') writing function"
  81.         Case setf=1 And pfilefilenameset=1
  82.             If pfileOldFilename="" And Txt$<>""
  83.                    pfileFilename=Txt$
  84.                 Else
  85.                    RuntimeError " Close the filehandle with PrintF('''') before setting a new filename !"
  86.                 EndIf
  87.         End Select
  88. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal