The
w+
mode in C is used to open a file for both reading and writing, with some specific features.- If the file does not exist,
w+
creates a new file. - If the file already exists, the content of the file is erased (truncated to zero length).
- You can both read from and write to the file.
- The file pointer is placed at the beginning of the file.
- Opening a file with
w+
will truncate the file to zero length. This means all existing data in the file will be erased.
Example
#include<stdio.h>
#include<conio.h>
int main(){
FILE *fp;
char str[20];
char ch;
fp = fopen("D://program.txt", "w+");
if (fp == NULL)
{
printf("Error");
exit(1);
}
fputs("The correct place for easy learning",fp);
rewind(fp);
while(!feof(fp))
{
ch = fgetc(fp);
printf("%c",ch);
}
printf("\n Data written successfully");
fclose(fp);
getch();
return 0;
}
Output
No comments:
Post a Comment