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

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

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 5690 | 回復(fù): 3
打印 上一主題 下一主題

[C] fsync調(diào)用完之后為什么fd就廢了? [復(fù)制鏈接]

論壇徽章:
3
亥豬
日期:2013-08-28 12:50:23白羊座
日期:2013-11-25 12:55:50酉雞
日期:2014-02-12 10:46:13
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2013-08-07 16:54 |只看該作者 |倒序?yàn)g覽
fsync調(diào)用完之后為什么fd就廢了?
fsync 之后 調(diào)read 時把fd傳進(jìn)去 返回-1 錯誤打印“Bad file number”
不知道fsync對fd做了什么。。。。。。
代碼如下:
  1. int main()

  2. {
  3.         int fd,byteswrite,bytesread;
  4.         char buf[256];
  5.         int length;
  6.         memset(buf,0,256);
  7.         strcpy(buf,"hello ,this is a test");
  8.         length = strlen(buf);
  9.         fd = creat("test.txt", O_RDWR|0777);
  10.         byteswrite = write (fd,buf,length);
  11.         printf("byteswrite is %d \n",byteswrite);
  12.         if(fsync(fd)==-1)
  13.                 printf("fsync error\n");
  14.         struct stat stat_buf;
  15.         int ret;
  16.         ret = fstat(fd,&stat_buf);
  17.         if(ret == -1){
  18.             printf("%s \n",strerror(errno));
  19.             exit(1);
  20.         }
  21.         memset(buf,0,256);
  22.         printf(" fd : %d \n",fd);
  23.         //close(fd);
  24.         //fd = open("test.txt",O_RDWR,0);
  25.         bytesread = read(fd,buf,length);
  26.         if(bytesread == -1){
  27.             printf("%s \n",strerror(errno));
  28.             exit(1);
  29.         }
  30.         printf("bytesread is %d \n",bytesread);
  31.         printf("read result is %s\n",buf);

  32. }
復(fù)制代碼
執(zhí)行結(jié)果:
  1. byteswrite is 21
  2. fd : 3
  3. Bad file number
復(fù)制代碼
該問題源自一老帖子http://72891.cn/thread-617996-1-1.html,看了之后就郁悶了{(lán):3_190:} 。。。今兒舊帖重貼,大神們指點(diǎn)指點(diǎn) {:3_193:}

論壇徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46處女座
日期:2013-10-24 14:25:01酉雞
日期:2014-04-07 11:54:15
2 [報告]
發(fā)表于 2013-08-07 17:14 |只看該作者
fd = creat("test.txt", O_RDWR|0777);

至少creat第二個參數(shù)是不能填O_RDWR的.

另外, creat打開是不可讀的.
creat is equivalent to open with flags equal to O_CREAT|O_WRONLY|O_TRUNC.

論壇徽章:
3
亥豬
日期:2013-08-28 12:50:23白羊座
日期:2013-11-25 12:55:50酉雞
日期:2014-02-12 10:46:13
3 [報告]
發(fā)表于 2013-08-08 09:13 |只看該作者
linux_c_py_php

朋友真是一針見血啊   {:3_182:}
第一點(diǎn) 
至少creat第二個參數(shù)是不能填O_RDWR的.
 的確是我疏忽了,這個老接口被后來的OPEN給兼容了,我以為參數(shù)也是一致的^_^
第二點(diǎn) 
另外, creat打開是不可讀的.
 我更沒留意了,嘿 
原因找出來了,應(yīng)該就是因?yàn)閏reat返回的FD是只寫的,對于讀接口來說是非法的,所以如果為讀專門open一次,返回一個供read的FD,應(yīng)該就沒問題了。總之是由于讀寫共用一個FD,搞混亂了。。。如果在之前的基礎(chǔ)上稍微改一下,應(yīng)該還是行得通的  嘿
        int fd,byteswrite,bytesread;
        char buf[256];
        int length;
        memset(buf,0,256);
        strcpy(buf,"hello ,this is a test";
        length = strlen(buf);
//        fd = creat("test.txt", S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
        fd = open("test.txt",O_CREAT|O_TRUNC|O_RDWR,0666);
        byteswrite = write (fd,buf,length);
        printf("byteswrite is %d \n",byteswrite);
        if(fsync(fd)==-1)
                printf("fsync error\n";
        struct stat stat_buf;
        int ret;
        ret = fstat(fd,&stat_buf);
        if(ret == -1){
            printf("%s \n",strerror(errno));
            exit(1);
        }
        memset(buf,0,256);
        printf(" fd : %d \n",fd);
        //close(fd);
        //fd = open("test.txt",O_RDWR,0);
        int offset;
        offset = lseek(fd,0,SEEK_CUR);
        printf(" offset : %d \n",offset);
        offset = lseek(fd,(-1)*offset,SEEK_CUR);
        if(offset == -1){
            printf("%s \n",strerror(errno));
            exit(1);
        }
        bytesread = read(fd,buf,length);
        if(bytesread == -1){
            printf("%s \n",strerror(errno));
            exit(1);
        }
        printf("bytesread is %d \n",bytesread);
        printf("read result is %s\n",buf);

多謝朋友指正

論壇徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46處女座
日期:2013-10-24 14:25:01酉雞
日期:2014-04-07 11:54:15
4 [報告]
發(fā)表于 2013-08-08 12:44 |只看該作者
解決了就好啊
您需要登錄后才可以回帖 登錄 | 注冊

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

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP