亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 2007 | 回復(fù): 4
打印 上一主題 下一主題

[Linux] [結(jié)貼]dup2重定向標(biāo)準(zhǔn)輸出失敗了,為什么呢? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2015-04-29 13:05 |只看該作者 |倒序?yàn)g覽
本帖最后由 wqrocdr 于 2015-04-30 09:14 編輯

我嘗試重定向標(biāo)準(zhǔn)輸出到一個(gè)文件里面,如下所示的程序:

  1. #include<sys/types.h>
  2. #include<sys/stat.h>
  3. #include<unistd.h>
  4. #include<fcntl.h>
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. int main(int argc, char *argv[])
  8. {
  9.     int fd=open("x.txt",O_CREAT|O_RDWR|S_IWUSR|S_IRUSR|S_IRGRP|S_IWGRP);
  10.     dup2(1,fd);
  11.     write(1,"hello world\n",12);
  12.     printf("myself\n");
  13.     close(fd);
  14.     return 0;
  15. }
復(fù)制代碼
編譯運(yùn)行,命令行輸出

  1. ~$ g++ n.cpp && ./a.out
  2. hello world
  3. myself
復(fù)制代碼
并沒(méi)有如我所期望的被重定向到了一個(gè)文件里面去。
而且這個(gè)新創(chuàng)建的x.txt文件的權(quán)限,看起來(lái)也沒(méi)有我所設(shè)置的權(quán)限,而且很奇怪的多了一個(gè)"set-user-id"位:

  1. -rwS-wx---  1 a    a       0  4月 29 13:03 x.txt*
復(fù)制代碼
這是為什么呢? 我的程序錯(cuò)在哪里

論壇徽章:
2
2015年亞洲杯之烏茲別克斯坦
日期:2015-04-15 15:43:482015亞冠之迪拜阿赫利
日期:2015-06-30 20:36:46
2 [報(bào)告]
發(fā)表于 2015-04-29 17:38 |只看該作者

  1. dup2(fd,1);
復(fù)制代碼
reference to the man page of dup2
int dup2(int oldfd, int newfd);
dup2() makes newfd be the copy of oldfd, closing newfd first if necessary.

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2015-04-29 18:47 |只看該作者
bfdhczw 發(fā)表于 2015-04-29 17:38
reference to the man page of dup2
int dup2(int oldfd, int newfd);
dup2() makes newfd be the copy o ...


我按照你說(shuō)的改了,如下:

  1. [a@localhost ~]$ cat n.cpp
  2. #include<sys/types.h>
  3. #include<sys/stat.h>
  4. #include<unistd.h>
  5. #include<fcntl.h>
  6. #include<stdio.h>
  7. #include<stdlib.h>
  8. int main(int argc, char *argv[])
  9. {
  10.     int fd=open("x.txt",O_CREAT|O_RDWR|S_IWUSR|S_IRUSR|S_IRGRP|S_IWGRP);
  11.     dup2(fd,1);
  12.     write(1,"hello world\n",12);
  13.     printf("myself\n");
  14.     close(fd);
  15.     return 0;
  16. }
  17. [a@localhost ~]$ g++ n.cpp && ./a.out
  18. hello world
  19. myself
復(fù)制代碼
可以看到結(jié)果還是不變啊? 這到底是怎么回事?

論壇徽章:
2
2015年亞洲杯之烏茲別克斯坦
日期:2015-04-15 15:43:482015亞冠之迪拜阿赫利
日期:2015-06-30 20:36:46
4 [報(bào)告]
發(fā)表于 2015-04-29 18:52 |只看該作者
回復(fù) 3# wqrocdr

你是不是x.txt已經(jīng)存在了,導(dǎo)致open失。
   

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2015-04-30 09:14 |只看該作者
bfdhczw 發(fā)表于 2015-04-29 18:52
回復(fù) 3# wqrocdr

你是不是x.txt已經(jīng)存在了,導(dǎo)致open失。


你說(shuō)的對(duì),確實(shí)應(yīng)該先刪除。
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP