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 ...
随机推荐
- IOS网络第二天 - 01-基本的HTTP请求
***************** #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @int ...
- angularjs - 415 (Unsupported Media Type)
angularJs+springMVC angular表单提交一个user实体时,报 angularjs - 415 (Unsupported Media Type)错误!! 原因是$http({ u ...
- ScriptManager.RegisterStartupScript
如果页面中不用Ajax,cs中运行某段js代码方式可以是:Page.ClientScript.RegisterStartupScript(Page.GetType(), "", & ...
- 【C51】UART串口通信
我们常需要单片机和其他模块进行通信,数据传输,常用的方式就是串口通信技术. 常用来 单片机<-->电脑, 单片机<-->单片机之间通信. 串行通信 versus 并行通信 并 ...
- ASP.NET 访问路径 错误提示 HTTP 错误 404.8 原来路径中包含bin目录被拒绝
HTTP 错误 404.8 - Not Found HTTP 错误 404.8 - Not Found 请求筛选模块被配置为拒绝包含 hiddenSegment 节的 URL 中的路径. 最可能的原因 ...
- home page
How To Set Your Home Page Step 1 – Navigate to Settings > Reading tab. Step 2 – Select A Static P ...
- LeetCode Missing Ranges
原题链接在这里:https://leetcode.com/problems/missing-ranges/ 题目: Given a sorted integer array where the ran ...
- sql操作之修改记录值
mysql修改.删除数据记录 用update修改记录 UPDATE tbl_name SET 要更改的列 WHERE 要更新的记录 这里的 WHERE 子句是可选的,因此如果不指定的话,表中的每个记录 ...
- asp检测数字类型函数
'**************************************************'函数ID:0014[检测ID是否为数字类型]'函数名:JCID'作 用:检测ID是否为数字类型' ...
- 商人过河问题(DFS)
问题描述:3个商人带着3个仆人过河,过河的工具只有一艘小船,只能同时载两个人过河,包括划船的人.在河的任何一边,只要仆人的数量超过商人的数量,仆人就会联合起来将商人杀死并抢夺其财物,问商人应如何设计过 ...