How to use freopen() function to reopen a file with different mode and close the old file in C

1 Answer

0 votes
#include <stdio.h>

int main(void)
{
	FILE *fp = freopen("d:\\data.txt", "w+", stdout);
    if (fp == NULL) 
	{
		perror("Error open file");
		return 1;
	}
	printf("This text is written to data.txt file\n");

	fclose(fp);

    return 0;
}

// file content: This text is written to data.txt file
  
/*
run:
  

*/

 



answered May 4, 2016 by avibootz

Related questions

1 answer 132 views
1 answer 182 views
4 answers 300 views
1 answer 213 views
...