File
handling in c:
- So far, to write programs are done on command prompt or terminal (i.e. ubuntu os of cmd) which is not stored anywhere and most of the time we want to rewrite those programs.
- But now a day we are using GUI level so we can easily store data in files, write also can be open and read.
- If needs to store and fetched data in the file on your system, different operations are required to store by using ‘C’ language.
*Creation of a
new file (fopen )
*Opening an existing
file (fopen).
*Reading from
file (fscanf or fgets).
*Writing to a
file (fprintf or fputs).
*Moving to a
specific location in a file (fseek, rewind).
*Closing a file
(fclose).
- C provides the number of functions that help to perform basic file operations.
- File functions are,
- Also, need modes for writing or read a file:
How
to Create a File:
- fopen is the standard function that is used to open a file.
- If the file is not present on the system, then it is created and then opened.
- If a file is already present on the system, then it is directly opened using this function.
Syntax:
FILE *fp;
fp = fopen ("file_name", "mode");
where, fp is a pointer variable.
FILE *fp;
fp = fopen ("file_name", "mode");
where, fp is a pointer variable.
Example:
Output:
#include
<stdio.h>
#include<conio.h>
int main() {
FILE *fp;
clrscr();
fp =
fopen ("welcome.txt", "w");//create new file
printf("\n File welcome.txt has been
created");
getch();
return 0;
}
#include
<stdio.h>
#include<conio.h>
int main() {
FILE *fp;
clrscr();
fp =
fopen ("welcome.txt", "w");//create new file
printf("\n File welcome.txt has been
created");
getch();
return 0;
}
- The close file is used to close the opened file.
Syntax:
fclose (file_pointer);
fclose (file_pointer);
Example:
FILE *fp;
fp = fopen ("file1.txt", "r");
fclose (fp);
FILE *fp;
fp = fopen ("file1.txt", "r");
fclose (fp);
How
to writing to a File:
- The write mode is used to write a given content into the file.
- fputc(char,
file_pointer):
It writes a character to the file pointed to by file pointer.
- fputs(str,
file_pointer):
It writes a string to the file pointed to by file pointer.
- fprintf(file_pointer,
str, variable_lists):
It prints a string to the file pointed to by file pointer. The string can
optionally include format specifiers and a list of variables variable lists.
Syntax:
pointer_variable =fopen(“file_name.txt”,”w”);
fputc(array_variable, pointer_variable);//if using loop
fputs("text or string", pointer_variable);//without loop
pointer_variable =fopen(“file_name.txt”,”w”);
fputc(array_variable, pointer_variable);//if using loop
fputs("text or string", pointer_variable);//without loop
Example program for create the file and write data into the file:
Output:
#include
<stdio.h>
#include<conio.h>
int main() {
int i;
FILE * fptr;//declare pointer variable to file
char fn[50];
char str[] = "C file
hadling\n";
fptr = fopen("file1.txt",
"w"); // "w" defines
"writing mode"
for (i = 0; str[i] != '\n'; i++) {
//write
to file using fputc() function
fputc(str[i], fptr);
}
fclose(fptr);//close the file
return 0;
}
#include
<stdio.h>
#include<conio.h>
int main() {
int i;
FILE * fptr;//declare pointer variable to file
char fn[50];
char str[] = "C file
hadling\n";
fptr = fopen("file1.txt",
"w"); // "w" defines
"writing mode"
for (i = 0; str[i] != '\n'; i++) {
//write
to file using fputc() function
fputc(str[i], fptr);
}
fclose(fptr);//close the file
return 0;
}
Example program for w+ mode:
#include
<stdio.h>
int main() {
FILE * fp;
fp = fopen("FILE1.txt",
"w+");
fputs("w+ is used to read and
write operations.", fp);
fputs("We don't need to use for
loop\n", fp);
fputs("Easier than fputc
function\n", fp);
fclose(fp);
return (0);
#include
<stdio.h>
int main() {
FILE * fp;
fp = fopen("FILE1.txt",
"w+");
fputs("w+ is used to read and
write operations.", fp);
fputs("We don't need to use for
loop\n", fp);
fputs("Easier than fputc
function\n", fp);
fclose(fp);
return (0);
Output:
How
to read file:
- Reading file is nothing but, it is used to show on the output screen to know what content inside the file.
Syntax:
pointer_variable =fopen(“file_name.extention”,”r”);
while( variable_name != EOF)
{
variable_name = fgetc(pointer_variable);
}
pointer_variable =fopen(“file_name.extention”,”r”);
while( variable_name != EOF)
{
variable_name = fgetc(pointer_variable);
}
Example program for read file mode:
Output:
#include
<stdio.h>
#include<conio.h>
int main()
{
FILE *fr;
char c;
clrscr();
fr = fopen("f2.c", "r");
while( c != EOF)
{
c = fgetc(fr); // read from file
printf("%c",c); //display
on screen
}
getch();
fclose(fr);
return 0;
}
#include
<stdio.h>
#include<conio.h>
int main()
{
FILE *fr;
char c;
clrscr();
fr = fopen("f2.c", "r");
while( c != EOF)
{
c = fgetc(fr); // read from file
printf("%c",c); //display
on screen
}
getch();
fclose(fr);
return 0;
}
Example program for write and read program:
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char s[30];
int n;
clrscr();
fp = fopen("output.txt", "w");
printf("\n Enter a string and a number:\n");
scanf("%s %d", s, &n); //read from keyboard
fprintf(fp, "%s %d", s, n); // write to file
fclose(fp);
fp = fopen("output.txt", "r");
printf("\n Read data from output.txt file..... \n");
fscanf(fr, "%s\n%d", s, &n); //read from file
printf("%s %d", s, n);
//display on screen
getch();
fclose(fp);
return 0;
}
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char s[30];
int n;
clrscr();
fp = fopen("output.txt", "w");
printf("\n Enter a string and a number:\n");
scanf("%s %d", s, &n); //read from keyboard
fprintf(fp, "%s %d", s, n); // write to file
fclose(fp);
fp = fopen("output.txt", "r");
printf("\n Read data from output.txt file..... \n");
fscanf(fr, "%s\n%d", s, &n); //read from file
printf("%s %d", s, n);
//display on screen
getch();
fclose(fp);
return 0;
}
How
to append text into an existing file:
- An append mode is nothing but, it similar to the write mode but it is used to concatenate the text already existing content.
- Write mode is used to write new content into the file.
Syntax:
pointer_variable =fopen(file_name_stored_variable,’a’);
fprintf(fp, "%s\n", content_store_variable);// Write content to file
fclose(fp);//after write the content then close the file
pointer_variable =fopen(file_name_stored_variable,’a’);
fprintf(fp, "%s\n", content_store_variable);// Write content to file
fclose(fp);//after write the content then close the file
Example:
Output:
#include
<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char ch;
char *filename = "file1.txt";
char *content = "This text is appended in file1 using C
programming.";
clrscr();
fp = fopen(filename, "r");//first
open and read file for display purpose
printf("\n Before append text into the file : %s \n",
filename);
while ((ch = fgetc(fp) )!= EOF)
{
printf ("%c", ch);
}
fclose(fp);//closed after read
fp = fopen(filename, "a");//again
open for append
fprintf(fp, "%s\n", content);// Write content to file
fclose(fp);//after write the
content then close the file
fp = fopen(filename, "r");//again
file open for read to check
append or not
printf("\n After append the text into the file %s \n",
filename);
while ((ch = fgetc(fp) )!= EOF)
{
printf ("%c", ch);
}
fclose(fp);//closed file
getch();
return 0;
}
#include
<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char ch;
char *filename = "file1.txt";
char *content = "This text is appended in file1 using C
programming.";
clrscr();
fp = fopen(filename, "r");//first
open and read file for display purpose
printf("\n Before append text into the file : %s \n",
filename);
while ((ch = fgetc(fp) )!= EOF)
{
printf ("%c", ch);
}
fclose(fp);//closed after read
fp = fopen(filename, "a");//again
open for append
fprintf(fp, "%s\n", content);// Write content to file
fclose(fp);//after write the
content then close the file
fp = fopen(filename, "r");//again
file open for read to check
append or not
printf("\n After append the text into the file %s \n",
filename);
while ((ch = fgetc(fp) )!= EOF)
{
printf ("%c", ch);
}
fclose(fp);//closed file
getch();
return 0;
}
Before running the
program:
After running the
program:
No comments:
Post a Comment