|Rapid-Q Documentation by William Yu (c)1999||Chapter 7|
fmCreate - Create file (read/write), truncate if file exists fmOpenRead - Open file for reading only fmOpenWrite - Open file for writing only fmOpenReadWrite - Open file for reading & writingTo open a file you need to pass the filename, and provide one of the above file modes.
DIM File AS QFileStream File.Open("Test.txt", fmOpenRead)This will open Test.txt for reading only. To read data, you use the methods ReadNum and ReadStr. See RAPIDQ.INC for valid numbers.
PRINT File.ReadNum(Num_SINGLE) '-- Read 4 bytes as a SINGLE number PRINT File.ReadStr(1024) '-- Read 1024 bytesAnother approach is to store the value into a variable, you can use the generic Read and Write routines.
File.Read(A&) '-- Read a long value and store in A& File.Write(B#) '-- Write a double value B# '-- or similarily, with more work: A& = File.ReadNum(Num_LONG) File.WriteNum(B#, Num_DOUBLE)After you're done with the file, make sure you close the handle. You can then reuse your file stream and open up another file. To test if you've reached the end of the file, you can either compare File.Position with File.Size or see if File.EOF is true (-1). So long as File.Position is less than File.Size, you haven't reached the end of the file. There are actually 2 ways to test if a file exists:
IF FileExists("Test.txt") <> FALSE THEN File.Open("Test.txt", fmOpenRead) END IFHowever, it's better just to cut this down to:
IF File.Open("Test.txt", fmOpenRead) = FALSE THEN ShowMessage("Could not open file") END IFFile.Open will return a non-zero number if the file was successfully opened, 0 otherwise.
DIM File AS QFileStream File.Open("test.txt", fmOpenRead) DIM Memory AS QMemoryStream Memory.CopyFrom(File, File.Size) '-- Copy everythingAs you can see, we've just copied the entire contents of our file "test.txt" to our memory stream. Now you can manipulate your memory stream without affecting the file.
TYPE TTest S AS STRING*8 N AS INTEGER END TYPE DIM Test AS TTest DIM File AS QFileStream File.Open("test.txt", fmCreate) File.WriteUDT(Test) File.CloseIn the above example, 12 bytes was saved to the file test.txt. 8 bytes for the fixed string S, and 4 bytes for N. To retrieve the UDT, use the method ReadUDT.
File.Open("test.txt", fmOpenRead) File.ReadUDT(Test)Saving/Loading arrays is equally easy.
DIM A(1 TO 100) AS LONG DIM File AS QFileStream File.Open("test.txt", fmCreate) File.SaveArray(A(1), 100) File.CloseThe first parameter of SaveArray is the starting array element to save, this could be any value from 1 to 100 in the above example. The next parameter specifies how many elements of the array to save. In this case we choose to save all 1..100 elements. Similarly, we can use LoadArray to retrieve our data:
File.Open("test.txt", fmOpenRead) File.LoadArray(A(1), 100)Streams are very popular in most high level languages for file/memory manipulation, but does take some time to get used to, especially if you're a die-hard BASIC user.
|Prev Chapter||Contents||Next Chapter|