歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> linux常用c函數:信號處理篇(四)

linux常用c函數:信號處理篇(四)

日期:2017/2/25 10:38:47   编辑:Linux教程
 返回值

  范例 #include<stdio.h>

  main()

  {

  FILE *fp;

  fp = fopen(“/tmp/noexist”,”r+”);

  if(fp = =NULL) perror(“fopen”);

  }

  執行 $ ./perror

  fopen : No such file or diretory

  strerror(返回錯誤原因的描述字符串)

  相關函數 perror

  表頭文件 #include<string.h>

  定義函數 char * strerror(int errnum);

  函數說明 strerror()用來依參數errnum的錯誤代碼來查詢其錯誤原因的描述字符串,然後將該字符串指針返回。

  返回值 返回描述錯誤原因的字符串指針。

  范例 /* 顯示錯誤代碼0 至9 的錯誤原因描述*/

  #include<string.h>

  main()

  {

  int i;

  for(i=0;i<10;i++)

  printf(“%d : %s\n”,i,strerror(i));

  }

  執行 0 : Success

  1 : Operation not permitted

  2 : No such file or directory

  3 : No such process

  4 : Interrupted system call

  5 : Input/output error

  6 : Device not configured

  7 : Argument list too long

  8 : Exec format error

  9 : Bad file descriptor

  mkfifo(建立具名管道)

  相關函數 pipe,popen,open,umask

  表頭文件 #include<sys/types.h>

  #include<sys/stat.h>

  定義函數 int mkfifo(const char * pathname,mode_t mode);

  函數說明 mkfifo()會依參數pathname建立特殊的FIFO文件,該文件必須不存在,而參數mode為該文件的權限(mode%~umask),因此 umask值也會影響到FIFO文件的權限。Mkfifo()建立的FIFO文件其他進程都可以用讀寫一般文件的方式存取。當使用open()來打開 FIFO文件時,O_NONBLOCK旗標會有影響

  1、當使用O_NONBLOCK 旗標時,打開FIFO 文件來讀取的操作會立刻返回,但是若還沒有其他進程打開FIFO 文件來讀取,則寫入的操作會返回ENXIO 錯誤代碼。

  2、沒有使用O_NONBLOCK 旗標時,打開FIFO 來讀取的操作會等到其他進程打開FIFO文件來寫入才正常返回。同樣地,打開FIFO文件來寫入的操作會等到其他進程打開FIFO 文件來讀取後才正常返回。

  返回值 若成功則返回0,否則返回-1,錯誤原因存於errno中。

  錯誤代碼 EACCESS 參數pathname所指定的目錄路徑無可執行的權限

  EEXIST 參數pathname所指定的文件已存在。

  ENAMETOOLONG 參數pathname的路徑名稱太長。

  ENOENT 參數pathname包含的目錄不存在

  ENOSPC 文件系統的剩余空間不足

  ENOTDIR 參數pathname路徑中的目錄存在但卻非真正的目錄。

  EROFS 參數pathname指定的文件存在於只讀文件系統內。

Copyright © Linux教程網 All Rights Reserved