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 ...
随机推荐
- JavaScript 回调函数中的 return false 问题
今天一个同事问了我一个问题,就是在 Ajax 方法中,请求成功后(success)的回调函数中根据响应的值来判断程序是否继续执行,他不解的是在回调函数中已经 return false 了,但是 Aja ...
- C#的TreeView标记
今天用到了TreeView控件,多次添加后发现内容是重复的,于是用到清除:this.myTreeView.Nodes.Clear(): 如果想在添加完节点后,默认全展开:this.myTreeView ...
- 10/12 study
[患者版]加号选择页: 这是四个TableView放在Scrollview上 上面是个xib封装的view 整体就是个scrollView,用xib摆上去的控件: 上面加了黄条,旧的控件统一修改y ...
- Where is the Global.asax.cs file
I am using VS 2008. I have created a new Asp.net web site project from File->New->Website-> ...
- MySQL乱码解决办法
MySQL支持几十种编码方式,并且默认的编码为:latinl,如果MySQL出现了乱码情况,不要慌乱,一下为你介绍两种解决编码方式: 在控制台输入命令,注意MySQL中的UTF-8写成utf8,例如我 ...
- SQLSERVER2012 附加数据库重名的问题
SQL2012附加数据库时如何更改数据库名称呢 方法一: 在“附加为”那一栏填写新的数据库名称就可以 方法二: 使用脚本 附加时点击脚本会自动生成附加脚本 直接修改database的名称即可!
- define宏定义和const常变量区别
1.define是宏定义,程序在预处理阶段将用define定义的内容进行了替换.因此程序运行时,常量表中并没有用define定义的常量,系统不为它分配内存.const定义的常量,在程序运行时在常量表中 ...
- delphi关闭程序Close,application.Terminate与halt区别
当Close是一个主窗体时,程序会退出.Close会发生FormClose事件,FormCloseQuery事件Halt会发生FormDestory事件,Application.Terminate以上 ...
- PRML读书笔记——Mathematical notation
x, a vector, and all vectors are assumed to be column vectors. M, denote matrices. xT, a row vcetor, ...
- bootstrap入门-3.响应式原理
Bootstrap网格系统(Grid System) 响应式网格系统随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列. 1 1 1 1 1 1 1 1 1 1 1 1 4 4 4 ...