文件的操作

来源:互联网 发布:纽约房价 知乎 编辑:程序博客网 时间:2024/05/21 10:29

本程序 旨在把argv[1]中的内容写入myfifo中

 

Code:
  1. #include <sys/types.h>  
  2. #include <sys/stat.h>  
  3. #include <errno.h>  
  4. #include <fcntl.h>  
  5. #include <stdio.h>  
  6. #include <stdlib.h>  
  7. #include <string.h>  
  8. #define FIFO_SERVER "/home/wangjian/wangjian/jctx/myfifo"   
  9.   
  10. int main(int argc,char* argv[])   
  11. {   
  12.  int fd, i = 0;   
  13.  char w_buf[100];   
  14.  int nwrite;   
  15.  FILE *p;   
  16.   
  17.  fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK|O_CREAT,0);   
  18.  if(fd==-1)   
  19.   if(errno==ENXIO)   
  20.    printf("open error; no reading process/n");   
  21.   else  
  22.    printf("other error [%d]/n",errno);   
  23.  if(argc==1)   
  24.  {   
  25.   printf("Please send something/n");   
  26.   exit(0);   
  27.  }   
  28.  if ((p = fopen(*++argv, "r")) == NULL)   
  29.   printf("error open argv[1]");   
  30.  while ((w_buf[i++] = getc(p)) != EOF)   
  31.   ;   
  32.  w_buf[i] = '/0';   
  33.  //strcpy(w_buf,p);   
  34.  //strcpy(w_buf,argv[1]);   
  35.  printf("w_buf : %s/n", w_buf);   
  36.  if((nwrite=write(fd,w_buf,strlen(w_buf)))==-1)   
  37.  {   
  38.   if(errno==EAGAIN)//资源暂时不可用   
  39.    printf("The FIFO has not been read yet.Please try later/n");   
  40.   else  
  41.    printf("other error [%d]/n",errno);   
  42.  }   
  43.  else    
  44.   printf("write %s to the FIFO/n",w_buf);   
  45.  exit(0);   
  46. }   

 

 

原创粉丝点击