Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

dup sample

Last updated at Posted at 2015-04-04

  #include <unistd.h>
  #include <fcntl.h>
  #include <sys/types.h>

  int screen;
  int file;
  int flags;
  mode_t mode;
  char data[] = "ok\n";
 
  /* 新規書き込み */
  flags |= O_WRONLY;
  flags |= O_TRUNC;
  flags |= O_CREAT;
  flags |= O_CLOEXEC;

  /* 0664 */
  mode |= S_IWUSR;
  mode |= S_IWGRP;

  /* 標準出力をオープン */ 
  screen = open("/dev/stdout", flags, mode); 

  /* 通常ファイルをオープン */
  file = open("hoge", flags, mode);
 
  /* 標準出力のディスクリプタと通常ファイルのディスクリプタを同期 */
  dup2(file, screen);

  /* 標準出力をクローズ */
  close(screen);
 
  /* この処理は通常ファイルへ書き込まれる */
  write(file, daa, sizeof data);


0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?