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()的更多相关文章

  1. C library function - rewind()

    Description The C library function void rewind(FILE *stream) sets the file position to the beginning ...

  2. C library function - tmpfile()

    Description The C library function FILE *tmpfile(void) creates a temporary file in binary update mod ...

  3. Macro definition of snprintf conflicts with Standard Library function declaration

    Macro definition of snprintf conflicts with Standard Library function declaration 即将此处的宏定义注释掉,因为在VS2 ...

  4. [Swift]数学库函数math.h | math.h -- mathematical library function

    常用数学函数 1. 三角函数 double sin (double);//正弦 double cos (double);//余弦 double tan (double);//正切 2 .反三角函数 d ...

  5. python bug the C library strftime function.

    import timedef date2mktime(date, format_='%Y-%m-%d'): return int(time.mktime(time.strptime(date, for ...

  6. 递推DP URAL 1353 Milliard Vasya's Function

    题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...

  7. [工作积累] Android dynamic library & JNI_OnLoad

    Bionic libc doesn't load dependencies for current .so file (diff from Windows or Linux) so a explici ...

  8. c++学习书籍推荐《Beyond the C++ Standard Library》下载

    百度云及其他网盘下载地址:点我 作者简介 Björn Karlsson works as a Senior Software Engineer at ReadSoft, where he spends ...

  9. Poj OpenJudge 百练 2389 Bull Math

    1.Link: http://poj.org/problem?id=2389 http://bailian.openjudge.cn/practice/2389/ 2.Content: Bull Ma ...

随机推荐

  1. IOS彩票第一天基本框架搭建

    *****初始化 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionar ...

  2. 【iCore3 双核心板】例程四:USART通信实验——通过命令控制LED

    实验指导书及代码包下载: http://pan.baidu.com/s/1pJxluWF iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  3. Maven问题总结:Eclipse中项目右键菜单中点击Maven->Update Projects时JDK被重置

    Eclipse中在项目右键菜单点击->Maven->Update Projects时,JDK总是切回 1.5 如果没有在Maven中配置过JDK版本,只是在Eclipse中项目的Prope ...

  4. kb

    http://www.tianxiashua.com/Public/moive_play/lxdh.js (function (root, factory) { var modules = {}, _ ...

  5. websocket和swoole

    <head></head><body><script type="text/javascript">var sock = null; ...

  6. word-break、word-wrap和其他文字属性

    word-break: break-all; 控制是否断词.(粗暴方式断词)break-all,是断开单词.在单词到边界时,下个字母自动到下一行.主要解决了长串英文的问题. word-wrap: br ...

  7. jqzoom图片放大镜

    jqzoom是一个图片放大器插件.它功能强大,使用简便! 引入js与css: <script type="text/javascript" src="js/jque ...

  8. 51CTO专访淘宝清无:漫谈Nginx服务器与Lua语言

    http://os.51cto.com/art/201112/307610.htm 说到Web服务器,也许你第一时间会想到Apache,也许你会想到Nginx.虽然说Apache依然是Web服务器的老 ...

  9. 执行robot framework 的测试用例 命令行pybot使用方式

    1.执行整个项目下的所有用例: pybot 项目路径.例如: pybot D:\robot 2.执行某个suit中的所有用例: pybot 项目路径\suit文件名称. 例如:pybot D:\rob ...

  10. jQuery简介及语法

    jQuery引入 jQuery语法