- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
fopen() for an existing file in write mode in C
The function fopen() opens the file pointed by pointer and read or write the file. In the write mode, “w” is used and in the read mode, “r” is used.
When a file exists in the directory, it treats as a new empty file and override the content of file by new data.
Here is the syntax of fopen() in C langauge,
FILE *fopen(const char *filename, const char *access_mode)
Here,
filename − The name of file which is to be opened.
acess_mode − The mode to access the file like read or write mode.
Here is an example of fopen() in C language,
Let’s say we have a file “one.txt” with the following content.
This is demo text! This is demo text! This is demo text!
Now, let us see the example.
Example
#include <stdio.h> #include<conio.h> void main () { FILE *f; int len; f = fopen("one.txt", "r"); if(f == NULL) { perror(“Error opening file”); return(-1); } fseek(f, 0, SEEK_END); len = ftell(f); fclose(f); printf("Size of file: %d bytes", len); getch(); }
Output
Size of file: 78 bytes
In the above program, a file type pointer variable is declared as f and it is used to open the file named as “one.txt” by using fopen() function.
FILE *f; int len; f = fopen("one.txt", "r");
Advertisements