close

unix c 開啟檔案

並把文件 123 內的內容輸出到螢幕上

用的是 open 而不是 fopen

 

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
char s[50];
int infile;
int count;

infile = open ("./123", O_RDONLY);   /* "./123" 是目前的工作目錄下的123檔案,  O_RDONLY 是指打開 123 之後,只做讀取的動作 open 回傳 -1 則代表錯誤*/
if (infile < 0) {
perror("open");
exit(1);
}

while (1) {
count = read(infile, s, sizeof s);
write(1, s, count);
if (count < 50) {
break;
}
}
return 0;
}

arrow
arrow
    全站熱搜

    inmyworld 發表在 痞客邦 留言(0) 人氣()