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. Day2(2016/1/22)——Testing

    Activity Button,Toast,Finish 显式intent 隐式明天再看…… 感觉要先补一补java……

  2. Construct Binary Tree from Inorder and Postorder Traversal || LeetCode

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * s ...

  3. C# mvc中为Controller或Action添加定制特性实现登录验证

    在本文开始前,先简单讲两个知识点: 1.每个action执行前都会先执行OnActionExecuting方法: 2.FCL提供了多种方式来检测特性的存在,比如IsDefined.GetCustomA ...

  4. C++程序设计(三)

    1. 运算符重载 目的:对抽象数据类型也能够直接使用C++提供的运算符.使得程序更简洁,代码更容易理解. 运算符重载的实质是函数重载 返回值类型 operator 运算符(形参表) { -- } 运算 ...

  5. 怎样将BigDecimal转换成Int

    BigDecimal a=new BigDecimal(12.88); int b=a.intValue(); System.out.println(b);//b=12;

  6. LeetCode Palindrome Permutation

    原题链接在这里:https://leetcode.com/problems/palindrome-permutation/ 题目: Given a string, determine if a per ...

  7. Jmeter关联

     一.Jmeter关联的方式: Jmeter中关联可以在需要获取数据的请求上 右键-->后置处理器 选择需要的关联方式,如下图有很多种方法可以提取动态变化数据: 二.正则表达式提取器: 1.比如 ...

  8. java 删除目录、 文件

    示例 import java.io.File; public class fileTest { public static void main(String []args){ String strVe ...

  9. bbs网站 models

    bbs网站 models #!/usr/bin/env python #_*_coding:utf-8_*_ from django.db import models from django.cont ...

  10. EBS安装过程报错,oracle.apps.fnd.txk.config.ProcessStateException: FileSys OS COMMAND Failed : Exit=2 See log for details.

    日志: Executing command: /test/software/12/startCD/Disk1/rapidwiz/jre/Linux_x64/1.6.0/bin/java -cp /te ...