- 論壇徽章:
- 0
|
本帖最后由 jun37213721 于 2011-04-11 22:48 編輯
為什么我用"w"方式打開(kāi)文件,用FD_ISSET來(lái)測(cè)試是否可讀,返回都是可讀形式呢?- int main()
- {
- int fd;
- FILE *fp;
- fp = fopen("log", "w");//以寫(xiě)方式打開(kāi)文件
- fd = fileno(fp);
- fd_set rsets;
- fd_set wsets;
-
- FD_ZERO(&rsets);
- FD_ZERO(&wsets);
- FD_SET(fd, &rsets);
- FD_SET(fd, &wsets);
- select(fd + 1, &rsets, &wsets, NULL, 0);
- if(FD_ISSET(fd, &rsets))
- { //不應(yīng)該執(zhí)行到這里才對(duì)啊,但確實(shí)進(jìn)來(lái)了
- printf("%d ready to read\n", fd);
- char buf[10000];
- fgets(buf, 10000, fp);//這里讀不到數(shù)據(jù)
- printf("%s\n", buf);//輸出為空的
- }
- if(FD_ISSET(fd, &wsets))
- { //正常執(zhí)行
- printf("%d ready to write\n", fd);
- char *tmp = "haha, I can write\n";
- write(fd, tmp, strlen(tmp));
- }
- fclose(fp);
- return 0;
- }
復(fù)制代碼 |
|