Once you open a file or have a file handle, you can read from and write to the file by using DosRead and DosWrite.
DosRead is called with a handle (obtained with DosOpen) for a file, pipe, or device. DosRead copies a specified number of bytes (up to the end of the file) from the file object to the buffer you specify. OS/2 returns, in a parameter, the number of bytes actually read (which might not be the same as the number of bytes requested because the end of the file might have been reached).
To read from a file, you must open it for reading or for reading and writing.
The following code fragment shows how to open the file named SAMPLE.TXT and read the first 512 bytes from it:
#define INCL_DOSFILEMGR /* File System values */ #include <os2.h> #define BUF_SIZE 512 HFILE hf; ULONG ulAction; BYTE abBuffer[BUF_SIZE]; ULONG cbRead; APIRET ulrc; ulrc = DosOpen("SAMPLE.TXT", &hf, &ulAction, 0, FILE_NORMAL, FILE_OPEN, OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, (PEAOP2) NULL); if (!ulrc) { DosRead(hf, abBuffer, sizeof(abBuffer), &cbRead); DosClose(hf); }
If the file does not have 512 bytes, DosRead reads to the end of the file and puts the number of bytes read in the cbRead variable. If the file pointer is already positioned at the end of the file when DosRead is called, the function puts a 0 in the cbRead variable.