Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Append

  1. #1

    Append

    Hey all, long time..

    Working on files,
    for append, does the file need to exist first?

    I have not tried yet.. but will..

    And can I put filename.txt?

    I'm trying to do record keeping for how long a computer runs on batteries. I want to append, but if need be, I will open and write current time and close.

    Does this work like apple basic in other words..
    What you open needs to be closed.. etc

    Old

  2. #2
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Rep Power
    159

    Re: Append

    This should do it and no you dont have to close the file. You can use any filename.****** you want.

    USES "FILE"
    
    DIM FileHandle as DWORD
    DIM Status   AS DWORD
    DIM TestString AS STRING
    DIM FileName  as STRING 
    DIM sMsg    as STRING
    
    TestString = "1234567890ABCDEF"      ' String to write to file
    FileName  = APP_SCRIPTPATH + "test.txt" ' Build filename
    
    Status = FILE_Append( FileName, TestString )
    
    TestString = "More Data"
    
    Status = FILE_Append( FileName, TestString )
    
    sMsg = FILE_Load(Filename)
    
    MsgBox 0, sMsg
    
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  3. #3

    Re: Append

    TY, so fast as always..
    I will write to program and test after lunch

    Old

  4. #4

    Re: Append

    Ok, here it is.. works great. the text.txt lives where the program runs from.

    ' Empty GUI script created on 11-13-2009 10:16:56 by (ThinAIR)
    ' Routine to write start time of program and 
    ' writes every 15 min
    ' W Little
    ' TY to Michael Clease for file operation
    uses "UI", "CONSOLE"
    USES "FILE"
    
    Dim Minutes  As long
    Dim Seconds  As Long
    Dim sMsg    As String
    Dim sMsgset  As String
    DIM errornumber as long
    DIM FileHandle as DWORD
    DIM Status   AS DWORD
    DIM TestString AS STRING
    DIM FileName  as STRING 
    
    '-----------------------------------------------------
    ' Start
    TestString = Time$      ' String to write to file
    FileName  = APP_SCRIPTPATH + "test.txt" ' Build filename
    Status = FILE_Append( FileName, "Time Started at -- "+ TestString )
    
    DO
      errorNumber = err
      'console_writeline err
    
    MINutes = mid$(Time$, 4, 2) ' Extract the Day from the returned string
    Seconds = mid$(Time$, 7, 4)
     
    'console_writeline MINutes
    sMsg = Minutes + Seconds
    
    IF Minutes = 00 and Seconds = 00 THEN
        Call cbPrintScreen
    end if
    IF Minutes = 15 and Seconds = 00 then
        Call cbPrintScreen
    End IF
    If Minutes = 30 and seconds = 00 then
        Call cbPrintScreen
    End IF
    IF Minutes = 45 and seconds = 00 then
        Call cbPrintScreen 
    end if
    
    
    
    CALLBACK FUNCTION cbPrintScreen() AS LONG
    if sMsg <> sMsgset then
      console_writeline time$
      
    TestString = Time$      ' String to write to file
    FileName  = APP_SCRIPTPATH + "test.txt" ' Build filename
     
    Status = FILE_Append( FileName, TestString )
    
    ' sMsg = FILE_Load(Filename)
     
      sMsgset = sMsg
    end if
    end function
    Loop
    
    Old

  5. #5
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Append

    Hi,

    interesting code, by using DO/LOOP you make sure the CPU will be used at nearly 99%, so PC seems "under use". Thanks for sharing!

    Few details you might consider for next versions or program (not necessary, they just make code shorter):
    - CALLBACK FUNCTION is thing used mainly in User Interface programming (they prefill CB* variables), you can leave it as FUNCTION
    - when FUNCTION does not return value, you can use SUB (like SUBroutine) instead
    - no need to use CALL keyword for common function calls
    - MOD function allows you to get reminder of division -> minutes/15 return 0 only for 0, 15, 30 and 45 minutes

    So the code can look like this too:
    ' Empty GUI script created on 11-13-2009 10:16:56 by (ThinAIR)
    ' Routine to write start time of program and 
    ' writes every 15 min
    ' W Little
    ' TY to Michael Clease for file operation
    Uses "UI", "CONSOLE"
    Uses "FILE"
    
    Dim Minutes  As Long
    Dim Seconds  As Long
    Dim sMsg    As String
    Dim sMsgset  As String
    Dim errornumber As Long
    Dim FileHandle As DWord
    Dim Status   As DWord
    Dim TestString As String
    Dim FileName  As String 
    
    '-----------------------------------------------------
    ' Start
    TestString = Time$      ' String to write to file
    FileName  = APP_ScriptPath + "test.txt" ' Build filename
    Status   = FILE_Append( FileName, "Time Started at -- "+ TestString )
       
    Do
     errorNumber = Err
     'console_writeline err
    
     Minutes = Mid$(Time$, 4, 2) ' Extract the Day from the returned string
     Seconds = Mid$(Time$, 7, 4)
     
     'console_writeline MINutes
     sMsg = Minutes + Seconds
     
     ' -- MOD gives remainder of division minutes / 15
     If Mod(minutes, 15) = 0 And Seconds = 0 Then
      cbPrintScreen()
     End If     
    Loop
    
    
    Sub cbPrintScreen() 
     If sMsg <> sMsgset Then
      Console_WriteLine Time$
       
      TestString = Time$      ' String to write to file
      FileName  = APP_ScriptPath + "test.txt" ' Build filename
      
      Status = FILE_Append( FileName, TestString )
     
      ' sMsg = FILE_Load(Filename)
      
      sMsgset = sMsg
     End If
    End Sub
    
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  6. #6

    Re: Append

    Hey Petr, long time

    You have me a bit confused at CPU at 99% and Under Used? I checked, with the programs running (our Dicom Scanner and this program) its 54%

    I tried the callback function but was using for/next and it would not continue. So I went to search and found DO/LOOP.

    Then I just worked from there..
    I will use yours next week when I recharge the battery for the next run.

    The program is being used on an ASUS B202 that we will be selling for what is called TMIFLASH for Sending WiFI and use of a flash drive to store medical images. But until we get an OK to use the AC from the medical workstation, we need to run off of batteries. Currently the last about 15 hrs or so. I just need proof. And I will not work for 15 hrs.
    I have looked over your program, and learn from it. shorted better in some cases.

    OLD

  7. #7
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Append

    Hi,

    54% ... that I would expect on dual core processors. I think your Atom is not dual core, but it has hyper threading, which might cause this.

    In case you would like low CPU usage, you might consider using DoEvents right after Do in the loop, on my PC it cut the usage to almost 0%:
    ' Empty GUI script created on 11-13-2009 10:16:56 by (ThinAIR)
    ' Routine to write start time of program and 
    ' writes every 15 min
    ' W Little
    ' TY to Michael Clease for file operation
    Uses "UI", "CONSOLE"
    Uses "FILE"
    
    Dim Minutes  As Long
    Dim Seconds  As Long
    Dim sMsg    As String
    Dim sMsgset  As String
    Dim errornumber As Long
    Dim FileHandle As DWord
    Dim Status   As DWord
    Dim TestString As String
    Dim FileName  As String 
                   
    '-----------------------------------------------------
    ' Start
    TestString = Time$      ' String to write to file
    FileName  = APP_ScriptPath + "test.txt" ' Build filename
    Status   = FILE_Append( FileName, "Time Started at -- "+ TestString )
       
    Do 
     DoEvents ' < ---
     errorNumber = Err
     'console_writeline err
    
     Minutes = Mid$(Time$, 4, 2) ' Extract the Day from the returned string
     Seconds = Mid$(Time$, 7, 4)
     
     'console_writeline MINutes
     sMsg = Minutes + Seconds
     
     ' -- MOD gives remainder of division minutes / 15
     If Mod(minutes, 15) = 0 And Seconds = 0 Then
      cbPrintScreen()
     End If     
    Loop
    
    
    Sub cbPrintScreen() 
     If sMsg <> sMsgset Then
      Console_WriteLine Time$
       
      TestString = Time$      ' String to write to file
      FileName  = APP_ScriptPath + "test.txt" ' Build filename
      
      Status = FILE_Append( FileName, TestString )
     
      ' sMsg = FILE_Load(Filename)
      
      sMsgset = sMsg
     End If
    End Sub
    
    If you want to try the maximum time with your EEE, lower the CPU usage is, longer it will stay awake.
    Another cleaner solution would be to create dialog with timer - it executes code when timer event is generated:
    Uses "UI", "File"
    
    ' -- ID numbers of controls
    Begin Const
     %bClose = 1000
     %eLog
     %tTimer 
    End Const    
    
    Global sMsg    As String
    Global sMsgset  As String
    Global errornumber As Long
    Global FileHandle As DWord
    Global TestString As String = Time$
    Global FileName  As String = APP_ScriptPath + "test.txt"
    Global Status   As DWord = FILE_Append( FileName, "Time Started at -- "+ TestString )
    Global hDlg As DWord
    
    ' -- Create dialog here
    Function TBMAIN()
    
    
     Dialog NEW 0, "Notebook Tester",-1,-1, 160, 120, _
            %WS_POPUP Or %WS_VISIBLE Or %WS_CAPTION Or %WS_SYSMENU Or %WS_MINIMIZEBOX To hDlg
     
     ' -- Place controls here
     Control ADD TEXTBOX, hDlg, %eLog, "Time Started at -- "+ TestString+$CRLF, 5, 5, 150, 90, %ES_MULTILINE
     Control ADD BUTTON, hDlg, %bClose, "Click to close", 95, 100, 60, 14, Call btnCloseProc
     
     Dialog SHOW MODAL hDlg, Call dlgProc
    
    End Function
    
    ' -- Callback for dialog
    CallBack Function dlgProc()
     Local Minutes, Seconds As Long
     ' -- Test for messages
     Select Case CBMSG
    
      Case %WM_INITDIALOG
      ' -- Put code to be executed after dialog creation here
       ' 30s timer
       Dialog SET Timer CBHNDL, %tTimer, 1000*30, 0
      
      ' -- Timer event is launched on timer tick 
      Case %WM_TIMER
       errorNumber = Err
         
       Minutes = Mid$(Time$, 4, 2) ' Extract the Day from the returned string
       Seconds = Mid$(Time$, 7, 4)
          
       sMsg = Minutes + Seconds
       
       ' -- MOD gives remainder of division minutes / 15
       If Mod(minutes, 15) = 0 And Seconds = 0 Then
        cbPrintScreen()
       End If   
    
      Case %WM_CLOSE
      ' -- Put code to be executed before dialog end here
       FILE_Append( FileName, "Program ended at "+Time$ )
       
     End Select
    
    End Function
     
    ' -- Callback for close button
    CallBack Function btnCloseProc()
    
     If CBMSG = %WM_COMMAND Then
      If CBCTLMSG = %BN_CLICKED Then
       ' -- Closes the dialog 
       Dialog End CBHNDL
      End If
     End If
    
    End Function
    
    Sub cbPrintScreen() 
     If sMsg <> sMsgset Then
      Control APPEND TEXT hDlg, %eLog, Time$+$CRLF
      
       
      TestString = Time$      ' String to write to file
      FileName  = APP_ScriptPath + "test.txt" ' Build filename
      
      Status = FILE_Append( FileName, TestString )
       
      sMsgset = sMsg
     End If
    End Sub
    
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  8. #8

    Re: Append

    Hey Petr,
    Ok, I used one of your samples and dropped my cpu from 54% to 3-4%..
    So the batteries lasted ~9hrs using my program, will see what happens by tomorrow on yours.. I can then make and avg.

    Thanks again.

    Now to see what you did..


  9. #9

    Re: Append

    Petr,
    Thought I let you know on the tests..

    54% battery died after 9 hrs 12v 10A /Hr battery pack

    3-4% Avg was 10 hrs and 20 mins

    The ASUS was running Windows XP stripped, with our software and the test sw.

    Quite good I say.

    Does DoEvents mean any event like IF's and alike?

    Old

  10. #10
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Append

    Hi,

    I am happy it worked, so your Atom PC can really live for almost 11 hours, impressive.

    DoEvents keyword "yields execution so that the operating system can process other events." That means all the CPU time is not consumed by parsing thinBASIC script and processing windows messages, but application behaves friendlier to other programs by letting the CPU breath a bit
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Page 1 of 2 12 LastLast

Similar Threads

  1. File Append?
    By kryton9 in forum thinBasic General
    Replies: 8
    Last Post: 21-03-2007, 01:09

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •