The wcBASIC OPEN command supports the following syntax: As a COMMAND (you set the filenum) OPEN file FOR mode [ACCESS access] <-- Access Restriction [lock] <-- Lock Type AS [#]filenum [LEN = reclen] <-- for mode random/binary As a FUNCTION (return file name), Recommended method. filenum = OPEN file FOR mode [ACCESS access] <-- Access Restriction [lock] <-- Lock Type [LEN = reclen] <-- for mode random/binary The Open statement syntax has these parts: Part Description ----- -------------- file Required: The file name (in DOS or WC:\ format) to open mode Required: Keyword specifying the file mode: Append open existing or new file to append Input open existing for reading only Output open new file for writing only Binary open file for reading/writing (stream) Random open file for reading/writing (records) access Optional. Keyword specifying the operations permitted on the open file by the owner process Read file is open for reading only Write file is open for writing only Read Write file is open for reading/writing lock Optional. Keyword specifying the operations restricted on the open file by other processes: Shared Lock Read Lock Write Lock Read Write filenum file number to assign to the file. If the AS clause is omitted, then this statement works like a function and returns a file number. This saves you from having to know which file number(s) are available, or using FreeFile to pick a free file number. reclen Optional. length of a record in a sequential or random-access file, in the range 1 through 32767. For files opened for random access, this value is the record length. o Notes: Text files are made up of Strings and numeric data, with carriage return and line feed characters added as they are written to the file. Text files are often called sequential files as the data is always added sequentially. The Output mode will create a new file whether it exists or not. It does not warn you if you are be about to overwrite an existing file. The Output mode will open the file for String or numeric data output using the Print statement. The Append mode will create a new file if one does not exist or prepare the existing file by positioning the file pointer at the end. The Append mode will open the file for String or numeric data output using the Print statement. The Input mode will open the file to input String or numeric data by using the Input statement. The file is existed to exist. Binary files are files with a record length of one byte (streams). Binary files are accessed either randomly or sequentially in one byte increments. A byte can be any value in the range 0 through 255. Random access files have a record length defined in the data type declared in your program. Use the Len(Type) statement to report the actual size - this is easier than trying to calculate it. A Random file's data must always be read by using the Get statement with the exact same record structure that was used to Put the data there in the first place. o Command vs Function Mode: The key difference is Error Handing. When command mode is used, an CATCH is required to trap any errors. As a function, you check the returned file number. Example of command mode: open "somefile.txt" for input as #1 ..... file is open .... close #1 end catch ERR_FILEOPEN print "Error opening file" end Example of function mode: dim h as integer h = open "somefile.txt" for input if (h <= 0) then ..... file is open .... close(h) else print "Error opening file" end end if o Examples: Open file streaming byte I/O in shared READ mode only. // testfileio-ex1.wcc dim h as integer h = open "wc:\data\somefile.txt" for binary access read lock write if (h > 0) then do while not eof(h) dim b as byte get #h, , b print "[";right(hex(b),2);"] "; if b >= 32 then print b; print loop close(h) else print "Error ";hex(GetLastError());" opening file" end if Here is an example using random access records: // testfileio-ex3.wcc #include "windows.wch" #include "htmlutil.wch" #include "util.wch" function myOpenData(fn as string, \ rlen as integer, \ w as integer \ ) as integer myOpenData = 0 dim h as integer dim err as integer dim e1 as integer = GetTickCount() do h = open fn for random lock write len = rlen err = GetLastError() if (h > 0) or (w <= 0) then exit do end if if (err <> 32) then exit do end if dim secs as integer = (GetTickCount()-e1)/1000 if (secs > w) then // timeout waiting to open file exit do end if // print "- file locked... waiting..." UpdateScreen() // Sleep(200) loop myOpenData = h SetLastError(err) end function //---------------------------------------------------- // MAIN //---------------------------------------------------- cls updatescreen() type TMyData status as integer f1 as string*255 f2 as string*255 end type dim cmd as string = getparamstr(paramstr(1),"cmd","put") dim rpos as integer = getparamint(paramstr(1),"rpos",0) dim f1 as string = getparamstr(paramstr(1),"f1","") dim f2 as string = getparamstr(paramstr(1),"f2","") dim data as tmydata dim h as integer dim fn as string = "wc:\data\testfileio-ex3-records.dat" h = myOpenData(fn, sizeof(TMyData), 10) if (h > 0) then dim nrecs as integer = lof(h) print "# of recs: ";nrecs select case(lcase(cmd)) case "put": print "Putting.... rec: ";rpos clear data data.status = rpos data.f1 = f1 data.f2 = f2 Put #h,rpos,data case "get": print "Getting.... rec: ";rpos clear data Get #h,rpos,data print "status: ";data.status print "f1 : ";data.f1 print "f2 : ";data.f2 end select WaitEnter() close(h) else print "Error ";hex(GetLastError());" opening file" end if How to test this example Test #1 - Testing File Locking during PUT process Open two DOS windows and switch to the WC folder in each, then type: Window #1 wcrun -r testfileio-ex3.wcx "cmd=put&rpos=10&f1=field1&f2=field2" Window #2 wcrun -r testfileio-ex3.wcx "cmd=get&rpos=10" In Window #2, you will see it waiting for 10 seconds to open the file. Just wait and you will see it finally error out. Repeat the command in Window #2 and before it times out, go to Window #1 and press ENTER. You will see the process in Window #2 finally complete.