perror().docVIP

  • 13
  • 0
  • 约1.42万字
  • 约 31页
  • 2016-10-06 发布于天津
  • 举报
perror().doc

perror() #includestdio.h   void perror(const char *s);   函数说明   perror ( )用来将上一个函数发生错误的原因输出到标准错误(stderr) 。参数s 所指的字符串会先打印出,后面再加上错误原因字符串。此错误原因依照全局变量errno 的值来决定要输出的字符串。 在库函数中有个error变量,每个error值对应着以字符串表示的错误类型。当你调用某些函数出错时,该函数已经重新设置了error的值。perror函数只是将你输入的一些信息和现在的error所对应的错误一起输出。 范例:   #include stdio.h   int main(void)   {   FILE *fp ;   fp = fopen( /root/noexitfile, r+ );   if ( NULL == fp )   {   perror(/root/noexitfile);   }   return 0;   }   运行结果:   [root@localhost io]# gcc perror.c   [root@localhost io]# ./a.out /root/noexitfile: No such file or directory freopen() 表头文件:#include stdio.d 定义函数:FILE *freopen(const char *, const char *mode, FILE *fp) 函数说明:参数字符串包含欲打开的文件路径及文件名,参数mode请参考fopen()说明。参数为已打开的文件指针。freopen()会将原所打开的文件流关闭,然后打开参数的文件简单说,就是实现重定向dup2; 把预定义的几个标准流文件(stdin, stdout, stderr)定向到由path指定的文件中。返回值 :文件顺利打开后,指向该流的文件指针就会被返回。若文件打开失败则返回NULL, 并把错误代码存在errno中范例: #include stdio.h main() { FILE *fp; fp = fopen(a.txt, r); fp = freopen(b.txt, r, fp); /* 改fp指向b.txt, 并关闭a.txt */ fclose(fp); } 附加一个代码模版:#include stdio.h int main() { freopen(xxx.in, r, stdin); freopen(xxx.out, w, stdout); /* 中间按原样写代码,什么都不用修改 */ fclose(stdin); fclose(stdout); return 0;} tips: 这样在用scanf输入时便不会从标准输入流提取数据。而是从in文件中获取输入。只要把输入事先粘贴到in。When a file is opened with update mode (+ as the second or third? ?character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an? ?intervening call to a file positioning function, unless the input? ?operation encounters endof-file. Opening (or creating) a text file with update mode may instead open (or create) a binary stream in some implementations. Getc与fgetc(宏与函数) The function getchar is defined to be equivalent to getc(stdin). The difference between the first two functio

文档评论(0)

1亿VIP精品文档

相关文档