Sunday, October 06, 2024

File Handling in C Language

File Handling in C Language 

File Handling


file handling is used for store a large amount of data in secondary file using different modes like write mode = "w"read mode  = "r" and append mode = "a" using different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), etc. file handling has the importance of reusability, portability, efficiency and storage. 

char range is= (0-255)
int range is = (-32768 - 32767)

File *fp range = unlimited.

program for file handling using *str i.e. unlimited string.

#include<stdio.h>
#include<conio.h>
main()
{
char *str;
clrscr();
printf("\n Enter the string= ");
fflush(stdin);
gets(str);
printf("\n The string is %s",str);
getch();
}

file handling for write mode

#include<stdio.h>
#include<conio.h>
main()
{
file *fp;
char ch;
clrscr();
fp=fopen("name.txt","w");
while((ch=getchar())!=EOF)
{
fputc(ch,fp);
}
fclose(fp);
}
getch();
}


file handling for append mode

#include<stdio.h>
#include<conio.h>
main()
{
file *fp;
char ch;
clrscr();
fp=fopen("name1.txt","a");
while((ch=getchar())!=EOF)
{
fput(ch,fp);
}
fclose(fp);
}
getch();
}


file handling for read mode

#include<stdio.h>
#include<conil.h>
main()
{
file *fp;
char ch;
clrscr();

fp=fopen("str1.c","r");
while((ch=getchar())!=EOF)
{
printf("%c",ch)
}
fclose(fp);
}
getch();
}

No comments:

Post a Comment

Autonomous Vehicles

Autonomous Vehicles Autonomous vehicles are cars or trucks that can drive themselves without needing a human to control them. They use advan...