fopen/freopen/fdopen
来源:互联网 发布:可靠性分析软件有哪些 编辑:程序博客网 时间:2024/05/20 19:28
fopen是用得比较多的,通常是打开文件读写。另外两个用得不多,但在系统编程时会用到。
freopen
通常与stdin,stdout,stderr一起用,有点重定向的味道
FILE *freopen(const char *restrict pathname, const char *restrict type,FILE *restrict fp);
- 例1
94 int a; 95 96 freopen("in.txt", "r", stdin); 97 while (scanf("%d", &a) != EOF) 98 printf("%d\n", a); 99 103 return 0;
从in.txt中读,重定向到stdin,所以下面scanf从stdin接收data内容。
- 例2
94 int a; 95 100 freopen("in.txt", "w", stdout);101 for (a = 0; a<6; a++)102 printf("%d\n", a);103 return 0;
这个例子正好与上面向抬,把stdout重定向到in.txt,所以printf输出的内容就不会在console显示,而被写到in.txt中去了。
fdopen
takes an existing file descriptor,which we could obtain from the open,dup,dup2,fcntl,pipe,socket,socketpair,or accept functions and associates a standard I/O stream with the descriptior.Thisfunction is often used with descriptors that are returned by the functions thatcreate pipes and network communication channels. Because these special typesof files cannot be opened with the standard I/O fopen function, we have to callthe device-specific function to obtain a file descriptor, and then associate thisdescriptor with a standard I/O stream using fdopen.
#include <stdio.h>FILE * fdopen(int fildes, const char * mode);
将文件描述符转成对应的句柄(指向文件的指针),比如下面是将标准输入stdin转为文件指针,会在console上输出hello!
106 FILE *fp = fdopen(0, "w+");107 fprintf(fp, "%s\n", "hello!");108 fclose(fp);
阅读全文
0 0
- fopen/freopen/fdopen
- fopen、freopen、fdopen and fclose
- fopen、freopen、fdopen打开流
- 5.5 fopen、freopen和fdopen函数-打开流
- open,fopen,fdopen
- fopen和freopen
- open、fopen与freopen
- open,fopen,freopen比较
- freopen与fopen的区别
- fdopen
- fdopen()
- fdopen
- fdopen()
- 详解C语言中的fopen()函数和fdopen()函数
- unix 网络编程 Fgets Fputs Fclose Fopen Fdopen
- 标准输入输出服从规则和fileno,freopen,fdopen,ftell,fseek,rewind几个函数
- APUE之标准I/O fopen,freopen,fread,fwrite实例。
- freopen和fopen的在ACM竞赛中的简单使用
- 注册页面
- 《老学长的第一次数学建模》
- 2.python里如何拷贝一个对象
- 步进与伺服电机区别与选型
- 笔记:生成的.9图为黑白色且不可编辑
- fopen/freopen/fdopen
- 招商银行信用卡中心笔试
- ubuntu 16.04 安装ROS依赖问题
- TensorFlow
- BZOJ 1806 IOI 2007 Miners 矿工配餐
- 解决send-mail: fatal: parameter inet_interfaces: no local interface found for ::1 没有本地接口发现::1
- tomcat中间件入门学习(一)
- fresco+fastjson
- Web前端