Advanced File handling operations in C-Important topics in C-computer science/Information technology
File handling in C
->Watch this for FUN - Funny job interview
Are you getting bored with online lectures?
->Watch this - When online lectures are boring
Here in this blog, we are going to learn how to handle any file on the Computer using this file handling in the C programming language tutorial. So, what's File handling in C?.
So, Let's get started with this.
File handling in C is an important concept in C programming language which is itself the mother language of all the high-level programming language. Through mastering this, you can master any file in your Personal computer with its content by writing, appending, and doing much more what you can.
A file represents a sequence of bytes on the disk where a group of related data is stored in the memory.
The file is created for the permanent storage of data.
It is a readymade structure.
In C language, we use a structure pointer of the file type to declare a file which is [FILE *fp] (where fp is the pointer to the characters in the file.).
Various operations that are used in this site are:
File handling in C is an important concept in C programming language which is itself the mother language of all the high-level programming language. Through mastering this, you can master any file in your Personal computer with its content by writing, appending, and doing much more what you can.
A file represents a sequence of bytes on the disk where a group of related data is stored in the memory.
The file is created for the permanent storage of data.
It is a readymade structure.
In C language, we use a structure pointer of the file type to declare a file which is [FILE *fp] (where fp is the pointer to the characters in the file.).
Various operations that are used in this site are:
2. fopen(" For opening the file or create a new file.").
3. fgets(" For getting the string.").
4. fprintf(" For printing in the file.").
5. fclose(" For closing the file.").
6. fputs(" For putting the string.").
7. Stdio(" Standard input/output.").
8. String(" String containing header file.").
9. fscanf("For taking the inputs from a file.").
There're three properties reading a file, writing a file, and appending in the file.
Although there're three more properties but quite uncommon, which are:-
Although there're three more properties but quite uncommon, which are:-
r+ – Opens a file for reading and write mode and sets the pointer to the first character in the file.
w+ –Opens a file for reading and write mode and sets the pointer to the first character in the file.
a+ – Opens a file for reading and write mode and sets the pointer to the first character in the file. But, it can’t modify existing contents.
1.File handling in C-Reading:
#include<stdio.h>
int main()
{
FILE *fp;
fp=fopen("Path of file","r"); //r is for read mode.
fp=fopen("Path of file","r"); //r is for read mode.
if(fp==NULL)
{
printf("File can't be found.");
}
else
{
int ch;
while((ch=fgetc(fp))!=EOF) // EOF for (End of file/End of giving input).
{
printf("%c",ch);
}
}
return 0;
}
}
<><><><><><><><><>
2.File handling in C-Writing.
#include<stdio.h>
#include<string.h>
#include<string.h>
int main()
{
FILE *fp;
fp=fopen("Path of file","w"); //w is for write mode.
if(fp==NULL)
{
printf("File can't be found.");
}
else
{
char s[1000]; // You can define any size to the array which depends on how much you write in the file.
fgets(s,sizeof(s),stdin);
fprintf(fp,"%s",s);
fclose(fp);
}
return 0;
}
<><><><><><><><><>
3.File handling in C-appending. ( appending is adding. )
#include<string.h>
int main()
{
FILE *fp;
fp=fopen("Path of file","a"); //a is for append mode.
if(fp==NULL)
{
printf("File can't be found.");
}
else
{
char s[1000]; // You can define any size to the array which depends on how much you add in the file.
int n, i;
scanf("%d",&n); // n is the number of lines that you want to add in the file.
for(i=0;i<n+1;i++)
{
fgets(s,"%s",stdin);
fputs(s,fp);
}
fclose(fp);
}
return 0;
}
<><><><><><><><><>
Syntax for using fscanf is:-
fscanf(fp,"%d",t); //where t is an identifier of any data type.
<><><><><><><><><>
Are you getting bored with online lectures?
Watch this - When online lectures are boring
Hope you have learned
something new today
HAVE A GOOD DAY.
Watch this for FUN - Funny job interview
<><><><><><><><><>
VISIT other
posts by clicking
on LABELS
for learning
more.
<><><><><><><><><>
Follow and don't
forget to
SUBSCRIBE.
<><><><><><><><><>
Site was good
ReplyDeleteBetter,
ReplyDeleteCan be made best
Good going bro👍👍
ReplyDeleteNice content
ReplyDeleteThnx everyone,
Deleteexpect to see such replies in the coming websites/blogs.
Outstanding
ReplyDeleteGreat blog! Well explained and worth reading
ReplyDeleteThat's Phenomenal bro!
ReplyDeleteExcept for the color
ReplyDeletecombination, everything was good.How did you build this blog by the way?
Blogging just depends on how creative you think.
Delete