Rapid-Q Documentation by William Yu (c)1999-2000 Appendix A: QFILESTREAM


QFILESTREAM Component

QFilestream is Rapid-Q's way of handling files. It's more elegant than the GET, and PUT method of traditional BASIC.

QFilestream Properties
Field Type R/W Default Support
EOF INTEGER R WXG
EOF specifies whether the file pointer has reached the end of file.
Handle INTEGER R W
LineCount INTEGER R WXG
LineCount determines how many lines there are in the file. Note that LineCount ignores the CR character, and will only detect LF, so it treats CRLF as one line, but LFLF as 2 lines.
Position INTEGER RW WXG
Position specifies the current position of the file pointer, any read/write operations will begin from here.
Size INTEGER R WXG
Size determines the size of the file in bytes.



QFilestream Methods

Method Type Description Params Support
Close SUB Close the stream 0 WXG
CopyFrom SUB (Stream, Bytes%) Copy from another stream
Either QFileStream or QMemoryStream
If Bytes%=0 then the whole stream is copied
2 W
Use CopyFrom to copy a QFILESTREAM or a QMEMORYSTREAM to the current filestream.
Details:
If Bytes% = 0 then the Stream is reset to position 0 and the whole stream is then copied.
Example:

$INCLUDE "RAPIDQ.INC"
DIM File1 AS QFILESTREAM
DIM File2 AS QFILESTREAM

File1.Open("test.txt", fmCreate)
File2.Open("oldtest.txt", fmOpenRead)
File1.CopyFrom(File2, 123) '-- Copy 123 bytes from File2

ExtractRes SUB (Resource AS LONG) Extract resource to filestream 1 WXG
Extracts a resource from your program to the filestream.
Details:
The parameter Resource is not the resource handle, but the absolute position of the resource within your file. This requires that you use the keyword Resource(n) to specify the resource number.
Example (dumping all resources):

$INCLUDE "RAPIDQ.INC"
$RESOURCE res_1 as "res.1"
$RESOURCE res_2 as "res.2"

DIM File1 AS QFILESTREAM

File1.Open("test.txt", fmCreate)

FOR I = 0 TO ResourceCount-1
    File1.ExtractRes(Resource(I))
NEXT

LoadArray SUB (Array(), NumElements&) Load data into array 2 WXG
Open SUB (FileName$, Method%) Open a file, Method% see RAPIDQ.INC '-- File Mode
CONST fmCreate = 65535
CONST fmOpenRead = 0
CONST fmOpenWrite = 1
CONST fmOpenReadWrite = 2

--------------------------------------------------

QFileStream is not a SUB as stated in the RQ HTML Documentation,
it's a FUNCTION which returns ZERO when the file dont on Open
Failure.

See example below.


2 WXG
Read SUB (variable) Generic Read, determines the storage space and saves data in variable 1 WXG
Datasize is automatically determined, and the value is stored into the variable.
Example:

$INCLUDE "RAPIDQ.INC"
DIM File1 AS QFILESTREAM
DIM B AS BYTE, I AS INTEGER

File1.Open("test.txt", fmOpenRead)
File1.Read(B) '-- Read a byte
File1.Read(I) '-- Read an integer

ReadLine FUNCTION () AS STRING Reads an entire line, supports UNIX files 0 WXG
ReadNum FUNCTION (Num_Type) AS DOUBLE Returns number    '-- ie. PRINT File.ReadNum(Num_SINGLE)
 

Num_Type can be next
CONST Num_BYTE = 1
CONST Num_SHORT = 2
CONST Num_WORD = 3
CONST Num_LONG = 4
CONST Num_DWORD = 5
CONST Num_SINGLE = 6
CONST Num_DOUBLE = 8
 

1 WXG
ReadBinStr FUNCTION (n) AS STRING Read n bytes, returns the binary string 1 WXG
ReadStr FUNCTION (n) AS STRING Read n bytes, returns the text string 1 WXG
ReadUDT SUB (MyType) Read and store data in a MyType structure 1 WXG
SaveArray SUB (Array(), NumElements&) Save array 2 WXG
Seek SUB (Position%, From%) Seek to Position%, From% see RAPIDQ.INC
'-- Offsets
CONST soFromBeginning = 0 '-- Seek (offset) from Beginning
CONST soFromCurrent = 1 '-- Seek (offset) from Current position
CONST soFromEnd = 2 '-- Seek (offset) from End
 
2 WXG
Write SUB (variable) Generic Write, determines the storage space and saves data to file 1 WXG
WriteLine SUB (S AS STRING) Writes string with CR+LF 1 WXG
WriteNum SUB (number, bytes%) Writes a number to file 2 WXG
WriteBinStr SUB (string, bytes%) Writes a binary string to file 2 WXG
WriteStr SUB (string, bytes%) Writes a text string to file (fast) 2 WXG
WriteUDT SUB (MyType) Write data stored in a MyType structure 1 WXG



QFileStream Examples

  $INCLUDE "RAPIDQ.INC"

  DIM File AS QFileStream

  File.Open("test.bas", fmOpenRead)
  S$ = File.ReadStr(File.Size)         '' Read entire file
  PRINT S$                             '' print it
  File.Close

'------------------------------------------------------
  $INCLUDE "RAPIDQ.INC"

  TYPE MyType
      Name AS STRING*30
      Phone AS STRING*8
      Age AS INTEGER
  END TYPE

  DIM Person AS MyType
      Person.Name = "Joe"
      Person.Phone = "555-5555"

  DIM File AS QFileStream
      File.Open("test", fmCreate)
      File.WriteUDT(Person)
'--------------------------------------------------------------------
Up to now 
QFilestream.Open ("MyFile", fmOpenRead) BUGGED at RUNTIME
when there is a 'problem' : file dont exist, file already opened, ...

Something like this can trap the error and save you !

' -----------------
     iOpenCount = 0
     Do
         iOpenCount ++
         Sleep (0.5)
         If iOpenCount = 4 Then ShowMessage ("Cannot Open File " & dumpPathFile): Exit Sub
     Loop Until dumpFile.Open (dumpPathFile, fmOpenRead)

' DO YOUR STUFF HERE

     dumpFile.Close
' ------------------

Maybe you all knew that except me :)

Jacques
Actually, the Open method of a QFileStream may fail for other reasons
than the file's not existing. For example, if another program has an
exclusive lock on the file, your program will not be able to open it.
Also, permissions may be set in such a way that your program could
write to the file but not open it to read it (on NTFS and Linux files
systems). Better error checking includes tests for both existence and
successful opening.

Stylistically, I dislike your code because I always use block IF
statements. However, this is not a requirement; I just find that it
follows the paradigm of structural programming better.

This is a skeleton of my file I/O error checking routines:
'Begin RapidQ Code
CONST ERR_CRITICAL = &H0001
CONST ERR_WARNING = &H0002
CONST ERR_INFO = &H0004
CONST ERR_QUESTION = &H0008
CONST ERR_ASSERT = &H1000
CONST ERR_FILE = &H2000
CONST ERR_INPUT = &H4000
CONST ERR_INTERNAL = &H8000
CONST ERR_EXCEPTION = &HF000

DECLARE SUB HandleError (message AS STRING, type AS INTEGER)

CONST FILENAME = "file.ext"
DIM file AS QFILESTREAM

IF FILEEXISTS (FILENAME) <> False THEN
IF File.Open (FileName, fmOpenRead) <> False THEN
'Do file stuff
File.Close
ELSE
HandleError ("The file could not be opened. Check your permissions.", ERR_CRITICAL OR ERR_FILE)
'More Stuff
END IF
ELSE
HandleError("The file could not be opened. Be sure it exists.", ERR_CRITICAL OR ERR_FILE)
'More Stuff
END IF
'End RapidQ Code

-- 
Chris


Prev Component Contents Next Component
Hosted by uCoz

>

Hosted by uCoz