/*-------------------------------------------------------------------*/ /* 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]; /* Read "somedata" 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. */ printf("%s\n", pcBuffer); return 0; } /* Sample execution: --> gcc -Wall -ansi -pedantic -o testlowlevelin testlowlevelin.c --> testlowlevelin somedata --> */