Important: The information in this document is obsolete and should not be used for new development.
Reading File Data
Once you have opened a file, you can read data from it by calling theFSReadfunction. Generally you need to read data from a file when the user first opens a file or when the user reverts to the last saved version of a document. TheDoReadFilefunction defined in Listing 1-8 illustrates how to useFSReadto read data from a file into a TextEdit record in either situation.Listing 1-8 Reading data from a file
FUNCTION DoReadFile (myWindow: WindowPtr): OSErr; VAR myData: MyDocRecHnd; {handle to a document record} myFile: Integer; {file reference number} myLength: LongInt; {number of bytes to read from file} myText: TEHandle; {handle to TextEdit record} myBuffer: Ptr; {pointer to data buffer} myErr: OSErr; BEGIN myData := MyDocRecHnd(GetWRefCon(myWindow)); {get window's data} myFile := myData^^.fileRefNum; {get file reference number} myErr := SetFPos(myFile, fsFromStart, 0); {set file mark at start} IF myErr <> noErr THEN BEGIN DoReadFile := myErr; Exit(DoReadFile); END; myErr := GetEOF(myFile, myLength); {get file length} myBuffer := NewPtr(myLength); {allocate a buffer} IF myBuffer = NIL THEN BEGIN DoReadFile := MemError; Exit(DoReadFile); END; myErr := FSRead(myFile, myLength, myBuffer); {read data into buffer} IF (myErr = noErr) OR (myErr = eofErr) THEN BEGIN {move data into TERec} HLock(Handle(myData^^.editRec)); TESetText(myBuffer, myLength, myData^^.editRec); myErr := noErr; HUnlock(Handle(myData^^.editRec)); END; DoReadFile := myErr; END;TheDoReadFilefunction takes one parameter specifying the window to read data into. This function first retrieves the handle to that window's document record and extracts the file's reference number from that record. ThenDoReadFilecalls theSetFPosfunction to set the file mark to the beginning of the file having that reference number. There is no need to check thatmyFilehas a nonzero value, becauseSetFPosreturns an error if you pass it an invalid file reference number.The second parameter to
SetFPosspecifies the file mark positioning mode; it can contain one of the following values:
CONST fsAtMark = 0; {at current mark} fsFromStart = 1; {set mark relative to beginning of file} fsFromLEOF = 2; {set mark relative to logical end-of-file} fsFromMark = 3; {set mark relative to current mark}If you specifyfsAtMark, the mark is left wherever it's currently positioned, and the third parameter ofSetFPosis ignored. The next three constants let you position the mark relative to either the beginning of the file, the logical end-of-file, or the current mark. If you specify one of these three constants, the third parameter contains the byte offset (either positive or negative) from the specified point. Here, the appropriate positioning mode is relative to the beginning of the file.If
DoReadFilesuccessfully positions the file mark, it next determines the number of bytes in the file by calling theGetEOFfunction. The key step in theDoReadFilefunction is the call toFSRead, which reads the specified number of bytes from the file into the specified buffer. In this case, the data is read into a temporary buffer; then the data is moved into the TextEdit record associated with the file. TheFSReadfunction returns, in themyLengthparameter, the number of bytes actually read from the file.