#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);
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.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme