C library function - freopen()
Description
The C library function FILE *freopen(const char *filename, const char *mode, FILE *stream) associates a new filename with the given open stream and at the same time closes the old file in the stream.
Declaration
Following is the declaration for freopen() function.
FILE *freopen(const char *filename, const char *mode, FILE *stream)
Parameters
filename − This is the C string containing the name of the file to be opened.
mode − This is the C string containing a file access mode. It includes −
mode | Description |
---|---|
"r" | Opens a file for reading. The file must exist. |
"w" | Creates an empty file for writing. If a file with the same name already exists then its content is erased and the file is considered as a new empty file. |
"a" | Appends to a file. Writing operations appends data at the end of the file. The file is created if it does not exist. |
"r+" | Opens a file to update both reading and writing. The file must exist. |
"w+" | Creates an empty file for both reading and writing. |
"a+" | Opens a file for reading and appending. |
stream − This is the pointer to a FILE object that identifies the stream to be re-opened.
Return Value
If the file was re-opened successfully, the function returns a pointer to an object identifying the stream or else, null pointer is returned.
Example
The following example shows the usage of freopen() function.
#include <stdio.h> int main ()
{
FILE *fp; printf("This text is redirected to stdout\n"); fp = freopen("file.txt", "w+", stdout); printf("This text is redirected to file.txt\n"); fclose(fp); return(0);
}
Let us compile and run the above program that will send the following line at STDOUT because initially we did not open stdout −
This text is redirected to stdout
After a call to freopen(), it associates STDOUT to file file.txt, so whatever we write at STDOUT that goes inside file.txt. So, the file file.txt will have the following content.
This text is redirected to file.txt
Now let's see the content of the above file using the following program −
#include <stdio.h> int main ()
{
FILE *fp;
int c; fp = fopen("file.txt","r");
while(1)
{
c = fgetc(fp);
if( feof(fp) )
{
break ;
}
printf("%c", c);
}
fclose(fp);
return(0);
}
C library function - freopen()的更多相关文章
- C library function - rewind()
Description The C library function void rewind(FILE *stream) sets the file position to the beginning ...
- C library function - tmpfile()
Description The C library function FILE *tmpfile(void) creates a temporary file in binary update mod ...
- Macro definition of snprintf conflicts with Standard Library function declaration
Macro definition of snprintf conflicts with Standard Library function declaration 即将此处的宏定义注释掉,因为在VS2 ...
- [Swift]数学库函数math.h | math.h -- mathematical library function
常用数学函数 1. 三角函数 double sin (double);//正弦 double cos (double);//余弦 double tan (double);//正切 2 .反三角函数 d ...
- python bug the C library strftime function.
import timedef date2mktime(date, format_='%Y-%m-%d'): return int(time.mktime(time.strptime(date, for ...
- 递推DP URAL 1353 Milliard Vasya's Function
题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...
- [工作积累] Android dynamic library & JNI_OnLoad
Bionic libc doesn't load dependencies for current .so file (diff from Windows or Linux) so a explici ...
- c++学习书籍推荐《Beyond the C++ Standard Library》下载
百度云及其他网盘下载地址:点我 作者简介 Björn Karlsson works as a Senior Software Engineer at ReadSoft, where he spends ...
- Poj OpenJudge 百练 2389 Bull Math
1.Link: http://poj.org/problem?id=2389 http://bailian.openjudge.cn/practice/2389/ 2.Content: Bull Ma ...
随机推荐
- bash 中的变量
bash 中的变量 Linux command line 笔记 变量无需声明,自动创建 var=abc #变量a赋值为字符串abc var="hello world" #字符串里有 ...
- CocoaPods 学习
参考文章 git address 1.简绍:CocoaPods是一个负责管理iOS项目中第三方开源代码的工具. 2.安装过程: $ sudo gem install cocoapods $ pod s ...
- Spark在Yarn上运行Wordcount程序
前提条件 1.CDH安装spark服务 2.下载IntelliJ IDEA编写WorkCount程序 3.上传到spark集群执行 一.下载IntellJ IDEA编写Java程序 1.下载IDEA ...
- java基础算法-快速排序
玩博客园很多年,第一次写点什么,就从基础开始吧.最近去面试,发现自己算法忘光了,赶紧复习下.以下代码自带测试类,复制进eclipse中右键 run as -->java application ...
- ActiveMq池
有两种连接方式 1.Spring 引用 <!-- 配置JMS连接工厂 --> <bean id="connectionFactory" class=" ...
- EF CodeFirst 如何通过配置自动创建数据库<当模型改变时>
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 本篇为进阶篇,也是弥补自己之前没搞明白的地方,惭愧 ...
- PinPhoto On OS X
把多张图片摆放到屏幕任何位置并保存成文件,下次打开时恢复之前的状态! 一般使用场景: 经常要看的图片,比如说软件快捷键.库的API.英文生词.常用配色等等摆好在屏幕上以便查看和记忆. 用PS等画画.设 ...
- java多线程编程--基础篇
一.基本概念 a.操作系统中进程与线程的概念 现在的操作系统是多任务操作系统.多线程是实现多任务的一种方式. 进程是指一个内存中运行的应用程序,每个进程都有自己独立的一块内存空间, 一个进程中可以启动 ...
- ms
meanShift的概念最早是由Fukunage[1]在1975年提出的,其最初的含义正如其名:偏移的均值向量:但随着理论的发展,meanShift的含义已经发生了很多变化.如今,我们说的meanSh ...
- MSDeploy
http://blogs.iis.net/jamescoo/default.aspx Web Deployment Tool Now Works With Credential Store Feb ...