/*------------------------------------------------------------------*/ /* testlowlevelout.c */ /* Author: Bob Dondero */ /* The creat, write, and close system calls. */ /*------------------------------------------------------------------*/ #include #include #include #include int main(int argc, char *argv[]) { int iFd; int iRet; ssize_t iBytesWritten; ssize_t iTotalBytesWritten; char pcBuffer[] = "somedata\n"; /* Write "somedata\n" to a file named tempfile. */ iFd = creat("tempfile", 0600); if (iFd == -1) {perror(argv[0]); return EXIT_FAILURE; } iTotalBytesWritten = 0; while (iTotalBytesWritten < 9) { iBytesWritten = write(iFd, pcBuffer + iTotalBytesWritten, (size_t)(9 - iTotalBytesWritten)); if (iBytesWritten == -1) {perror(argv[0]); return EXIT_FAILURE; } iTotalBytesWritten += iBytesWritten; } iRet = close(iFd); if (iRet == -1) {perror(argv[0]); return EXIT_FAILURE; } return 0; } /*------------------------------------------------------------------*/ /* Sample execution: $ gcc -Wall -ansi -pedantic testlowlevelout.c -o testlowlevelout $ testlowlevelout $ cat tempfile somedata */