How do I flush the write buffer after a write without closing and reopening the file?

Recommended Answers

All 2 Replies

Wow. That one made me think. The solution is to use pure API Calls to create the file. Which means, forget about the whole:

open "yada.txt" for input as #1
     'Stuff
close #1

You'll Have to use:

public Const GENERIC_WRITE = &H40000000
public Const GENERIC_READ = &H80000000
public Const FILE_ATTRIBUTE_NORMAL = &H80
public Const CREATE_ALWAYS = 2
public Const OPEN_ALWAYS = 4
public Const INVALID_HANDLE_VALUE = -1

public Declare Function ReadFile Lib "kernel32" _
    (ByVal hFile As Long, lpBuffer As Any, _
    ByVal nNumberOfBytesToRead As Long, _
    lpNumberOfBytesRead As Long, _
    ByVal lpOverlapped As Long) As Long


public Declare Function CloseHandle Lib "kernel32" _
    (ByVal hObject As Long) As Long

public Declare Function WriteFile Lib "kernel32" _
   (ByVal hFile As Long, lpBuffer As Any, _
    ByVal nNumberOfBytesToWrite As Long, _
    lpNumberOfBytesWritten As Long, _
    ByVal lpOverlapped As Long) As Long
    
public Declare Function CreateFile Lib _
   "kernel32" Alias "CreateFileA" _
   (ByVal lpFileName As String, _
   ByVal dwDesiredAccess As Long, _
   ByVal dwShareMode As Long, _
   ByVal lpSecurityAttributes As Long, _
   ByVal dwCreationDisposition As Long, _
   ByVal dwFlagsAndAttributes As Long, _
   ByVal hTemplateFile As Long) As Long
   
public Declare Function FlushFileBuffers Lib "kernel32" _
   (ByVal hFile As Long) As Long

Then use them in the code to open, write, flush, write and close the file.... I've always been a personal fan of the API :)

Thanks Comatose...you must be awesome when you are awake:)
guess it's time to take the plunge

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.