Previous Book Contents Book Index Next

Inside Macintosh: Files /
Chapter 1 - Introduction to File Management / Using Files

Reading File Data

Once you have opened a file, you can read data from it by calling the FSRead function. 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. The DoReadFile function defined in Listing 1-8 illustrates how to use FSRead to 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;
   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;
   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
         DoReadFile := myErr;

   myErr := GetEOF(myFile, myLength);           {get file length}
   myBuffer := NewPtr(myLength);                {allocate a buffer}
   IF myBuffer = NIL THEN
         DoReadFile := MemError;

   myErr := FSRead(myFile, myLength, myBuffer); {read data into buffer}
   IF (myErr = noErr) OR (myErr = eofErr) THEN
      BEGIN                                     {move data into TERec}
         TESetText(myBuffer, myLength, myData^^.editRec);
         myErr := noErr;
   DoReadFile := myErr;
The DoReadFile function 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. Then DoReadFile calls the SetFPos function to set the file mark to the beginning of the file having that reference number. There is no need to check that myFile has a nonzero value, because SetFPos returns an error if you pass it an invalid file reference number.

The second parameter to SetFPos specifies the file mark positioning mode; it can contain one of the following values:

   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 specify fsAtMark, the mark is left wherever it's currently positioned, and the third parameter of SetFPos is 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 DoReadFile successfully positions the file mark, it next determines the number of bytes in the file by calling the GetEOF function. The key step in the DoReadFile function is the call to FSRead, 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. The FSRead function returns, in the myLength parameter, the number of bytes actually read from the file.

Previous Book Contents Book Index Next

© Apple Computer, Inc.
2 JUL 1996