0是標準輸入STDIN_FILENO,1是標準輸出STDOUT_FILENO,2是標準錯誤輸出STDERR_FILENO
例1:向標準輸出文件輸出,寫文件
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //unistd.h 是 C 和 C++ 程序設計語言中提供對 POSIX 操作系統 API 的訪問功能的頭文件的名稱
#include<string.h>
void printArr(char* str);
int main(void)
{
char buf[100]={0};
strcpy(buf,"abcsdfggh\n");
//printf std for standard output,
//STDOUT_FILENO is a standard output file
//write(STDOUT_FILENO,buf,strlen(buf));
printArr(buf);
return 0;
}
void printArr(char* str)
{
if(str==NULL)
{
return;
}
write(STDOUT_FILENO,str,strlen(str));
}
例2:讀文件
int main(void)
{
char buf[100]={0};
read(STDIN_FILENO,buf,sizeof(buf));
//可以處理空格與越界(限制sizeof(buf),越界自動截取)
printf("%s\n",buf);
return 0;
}
打開文件描述符
需要包含以下三個頭文件:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
//mode = mode&~umask,即新的權限是所寫的權限與掩碼取反相與,是邏輯減法
655
022
655
如果mode爲655 它裏面並沒有022元素,故仍然爲655 (關於mode取它的補數)
int creat(const char *pathname, mode_t mode);
例:
int main(void)
{
umask(0);//重新指定掩碼值,使其不再從shell繼承0022
int fd = open("opentest3.txt",O_WRONLY|O_CREAT,0777);
if(fd == -1)
{
ERR_EXIT("open error");
}
printf("open success\n");
return 0;
}
flags指的是打開該文件的方式:
O_RDONLY:只讀
O_WRONLY:只寫
O_RDWR:讀寫
O_CREAT:如果不存在就創建一個
O_APPEND:追加
O_EXCL:如果文件存在,強制open失敗,與O_CREAT配合使用
O_TRUNC:在open時,將文件內容清空
O_SYNC:在write時,自動同步磁盤
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
//將數據寫入到內核緩衝區但還未同步到磁盤上,可以調用fsync函數進行同步,也可以在open時指定同步選項O_SYNC
int fsync(int fd);
int fdatasync(int fd);// 它只保證開打文件的數據全部被寫到物理設備上,但是一些元數據並不是一定的,這也是它與fsync的區別。
ssize_t read(int fd, void *buf, size_t count);
open成功後會返回一個文件描述符,失敗會返回-1,
並設置errno變量(需要添加errno.h頭文件)
read()會把參數fd所指的文件傳送nbyte個字節到buf指針所指的內存中。若參數nbyte爲0,則read()不會有作用並返回0。返回值爲實際讀取到的字節數,如果返回0,表示已到達文件尾或無可讀取的數據。錯誤返回-1,並將根據不同的錯誤原因適當的設置錯誤碼。
write()會把指針buf所指的內存寫入count個字節到參數fd所指的文件內。當然,文件讀寫位置也會隨之移動。
如果順利write()會返回實際寫入的字節數。當有錯誤發生時則返回-1,錯誤代碼存入errno中。
errno是一個數,它的不同值代表不同錯誤,可以使用strerror(errno)來讀取
例:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include<string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<errno.h>
int main(void)
{
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
/*將這三個關掉後打開的文件描述符從0開始不再是3
* 預先默認值是0 1 2 如果只將STDOUT_NO關掉
* 則1是空的那麼新打開的會佔用1描述符
* 可以ps -u 進程名 獲取進程ID
* 進入proc查看ID,進入ID
* 進入fd查看
*/
int i = open("hell.c",O_RDONLY);
if(i==-1)
{
printf("%s\n",strerror(errno));
}
else
{
printf("%d\n",i);
close(i);
}
return 0;
}
tty:終端命令
例:通過打開另一終端,將數據輸入到另一終端
int main(void)
{
close(STDOUT_FILENO);
int j = open("/dev/pts/0",O_WRONLY);//1
int i = open("hell.c", O_RDONLY);//3
if (i == -1)
{
printf("%s\n", strerror(errno));
} else
{
while(1)
{
printf("j==%d,i==%d\n",j,i);
sleep(1);//linux下sleep是以秒計時的,usleep(1000000)是以微秒計時的
}
close(i);
}
return 0;
}
//打開不定大小文件,並輸出到終端
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include <fcntl.h>
int main(int arg,char *args[])
{
if(arg<2)
return -1;
int fd = open(args[1],O_RDONLY);
if(fd==-1)
{
printf("error occur %s\n",strerror(errno));
}
else
{
printf("the fd = %d\n",fd);
char buf[100]={0};
while(read(fd,buf,sizeof(buf)-1) > 0)
//從fd讀,讀sizeof(buf)-1,防止將\0沖掉
{
printf("%s",buf);
memset(buf,0,sizeof(buf));
}
close(fd);
}
return 0;
}
//往文件寫
int main(int arg,char *args[])
{
if(arg<2)
{
printf("open file error");
return -1;
}
int fd = open(args[1],O_RDWR|O_APPEND);
if(fd==-1)
{
printf("error occur %s\n",strerror(errno));
}
else
{
printf("the fd = %d\n",fd);
char buf[100]={0};
strcpy(buf,"what a hell!,the world is hell\n");
int nbyte = write(fd,buf,strlen(buf));
if(nbyte<=0)
{
printf("end of file or error");
}
close(fd);
}
return 0;
}