システムコールwriteを使った簡単なプログラミング



/* simple_write.c*/
#include<unistd.h> /* write */
#include<stdlib.h> /* exit */
int main()
{
/*
#include <unistd.h>
  size_t write(int fildes, const void *buf, size_t nbytes);
 */
/*
#include <stdlib.h>
  void exit(int status);
*/

/*When a program starts, it usually has three of these descriptor
 already opened. 1:Standard input, 2:Standard output, 3:Standard error
 */

/* Simply prints a pessage on the standard output(filediscriptor is 1) */
if((write(1, "Here is some data\n", 18)) != 18 ){
write(2, "A write error has occurred on file descriptor 1 \n", 46);
exit(1);}
/* When a program exits, all open file descriptors are automatically closed,
 so we don't need to close them explicitly */
exit(0);
}