/*-------------------------------------------------------------------*/ /* testlowlevelin.c */ /* The open, read, and close system calls. */ /*-------------------------------------------------------------------*/ #include #include #include int main(int argc, char *argv[]) { int iFd; int iRet; int iBytesRead; char pcBuffer[9]; int i; /* Read "somedata\n" from a file named tempfile. */ iFd = open("tempfile", O_RDONLY); if (iFd == -1) {perror(argv[0]); return 1; } iBytesRead = 0; while (iBytesRead < 9) { iRet = read(iFd, pcBuffer + iBytesRead, 9 - iBytesRead); if (iRet == -1) {perror(argv[0]); return 1;} iBytesRead += iRet; } iRet = close(iFd); if (iRet == -1) {perror(argv[0]); return 1; } /* Print the data to verify that the previous worked. */ for (i = 0; i < 9; i++) putchar(pcBuffer[i]); return 0; } /* Sample execution: $ gcc -Wall -ansi -pedantic -o testlowlevelin testlowlevelin.c $ testlowlevelin somedata $ */