Results 1 to 9 of 9

Thread: File Append?

  1. #1
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    File Append?

    Hi Guys, is there a File Append type command I am not seeing.

    That is, I have a text based file, I just want to append content to it at the end WithOUT having to open it, load it up and then add the new content and then save it out again.

    Thanks for any info.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  2. #2
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Re: File Append?

    Ok, I see I can use File_Open with Append for the mode and then File_LinePrint and then File_Close. Should do the trick for now.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  3. #3
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Re: File Append?

    I can't get it to append. I tried seeking to the eof and still no luck.

    [code=thinbasic]
    local s as string
    local n as long
    s = Str$(planetColorX) + "," + Str$(planetColorY) + "," + Str$(planetColorZ) + "," + Str$(planetX) + "," + Str$(planetY)_
    + "," + BackgroundBMP + $crlf
    if (n = FILE_OPEN("LikeSavedSettings.txt", "Append")) then
    FILE_Seek(n, FILE_EOF) 'Just added this and still couldn't get it to work, thought it might need it
    FILE_LinePrint(n, s)
    FILE_CLOSE(n)
    end if[/code]

    Thanks in advance.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: File Append?

    Ken,

    the code
    [code=thinbasic]
    if (n = FILE_OPEN("LikeSavedSettings.txt", "Append")) then
    [/code]
    is not returning the file id but comparing the value of n (that is zero at that point) with the fileid returned by file_open.
    So you are not storing any file id anywhere.

    Also file_open with "APPEND" mode automatically set the file cursor at the end of the file and if the file does not exists, it is created.
    So, do it simple, step by step. Open the file and get file id. Test file id and if ok, write what you need and close.

    [code=thinbasic]
    USES "file"
    local s as string
    local FileID as long

    S="123" '--- Write here whatever

    FileID = FILE_OPEN(app_soucepath & "LikeSavedSettings.txt", "APPEND")
    if FileID then
    FILE_LinePrint(FileID, s)
    FILE_CLOSE(FileID)
    end if
    [/code]

    Another info: file_seek will not work for files open in append mode.

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  5. #5
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Re: File Append?

    Thanks Eros for the very clear explanation and example!!!
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  6. #6
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: File Append?

    Ken,

    in thinBasic an assignement directive starts with a variable.
    So,
    [code=thinbasic]
    n = FILE_OPEN("LikeSavedSettings.txt", "Append")
    [/code]
    is an assignment directive, while
    [code=thinbasic]
    if (n = FILE_OPEN("LikeSavedSettings.txt", "Append")) then
    [/code]
    is a comparison directive. N will not given a value but the value of N is compared with the value returned by File_Open but never assigned to N.

    Other languages have = or := to assign and == to compare. Or even other mix.
    In thinBasic we want it simple and easy to understand so most of the time it is better to just write different sentence: one for assignement and one for comparing.

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  7. #7
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Re: File Append?

    I agree, it does make it so much easier to follow what is happening. Look at the mistake I made otherwise, trying to combine it as I did. Thanks again Eros.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  8. #8
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: File Append?

    Ken,

    I've added FILE_Append function.
    Should be more easy to use.

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  9. #9
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Re: File Append?

    Thanks Eros, I love being spoiled with such goodies, couldn't be any easier!!
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

Similar Threads

  1. Append
    By oldpapa49 in forum Files and directories handling
    Replies: 10
    Last Post: 21-11-2009, 10:29

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
  •