C File Handling
C File Handling
- Seema Sharma
WHAT IS FILE
A collection of data or information that are stored
on a computer known as file.
Data files
Can be created, updated, and processed by C programs
Are used for permanent storage of large amounts of
data
WHY FILES ARE NEEDED?
When a program is terminated, the entire data is
lost. Storing in a file will preserve your data even if
the program terminates.
Text files
Binary files
TEXT FILES
Text files are the normal .txt files. You can easily
create text files using any simple text editors such
as Notepad.
Binary files
FILE OPERATIONS
Mode Description
r opens a text file in read mode
w opens a text file in write mode
a opens a text file in append mode
r+ opens a text file in read and write mode
w+ opens a text file in read and write mode
a+ opens a text file in read and write mode
fopen("file-name", "read-mode");
FOPEN
fopen("E:\\cprogram\\newprogram.txt","w");
fopen("E:\\cprogram\\oldprogram.bin","rb");
EXAMPLE
#include <stdio.h>
int main()
{
FILE *fp; fp = fopen ("data.
txt", "w");
}
#include <stdio.h>
int main()
{
FILE *fp; fp = fopen ("D://data.txt",
"w");
}
CLOSE A FILE
Example:
FILE *fp;
fp = fopen ("data.txt", "r");
fclose (fp);
WRITING TO A FILE
#include <stdio.h>
int main()
{
FILE *fptr;
fptr = fopen("fprintf_test.txt", "w");
// "w" defines "writing mode" /* write to file */
fprintf(fptr, "Learning C with JU");
fclose(fptr);
return 0; }
OUTPUT:
Learning C with JU
READING DATA FROM A FILE
fgetc(file_pointer):
It returns the next character from the file pointed to by the file
pointer. When the end of the file has been reached, the EOF is
sent back.
fgets(buffer, n, file_pointer):
It reads n-1 characters from the file and stores the string in a
buffer in which the NULL character '\0' is appended as the
last character.
{
FGETC ()
FILE * file_pointer;
char buffer[30], c;
file_pointer = fopen("fprintf_test.txt", "r");
printf("----read a line----\n");
fgets(buffer, 50, file_pointer);
printf("%s\n", buffer);
printf("----read and parse data----\n");
file_pointer = fopen("fprintf_test.txt", "r"); //reset the pointer
char str1[10], str2[2], str3[20], str4[2];
Output
fscanf(file_pointer, "%s %s %s %s", str1, str2, str3, str4);
printf("Read String1 |%s|\n", str1);
printf("Read String2 |%s|\n", str2);
-----read a line----
printf("Read String3 |%s|\n", str3);
Learning C with JU
printf("Read String4 |%s|\n", str4); ----read and parse data---- Read
printf("----read the entire file----\n"); String1 |Learning|
file_pointer = fopen("fprintf_test.txt", "r"); //reset the pointer Read String2 |C|
while ((c = getc(file_pointer)) != EOF) Read String3 |with|
printf("%c", c); Read String4 |JU|
fclose(file_pointer); ----read the entire file---- Learning C
return 0; } with Guru99
READ AND WRITE WITH GETC AND PUTC
#include <stdio.h>
int main()
{
FILE * fp;
char c; OUTPUT
printf("File Handling\n");
//open a file JU2020
fp = fopen("demo.txt", "w"); //writing operation ^Z
while ((c = getchar()) != EOF) Data entered
{ JU2020
putc(c, fp); } //close file
fclose(fp);
printf("Data Entered:\n");
//reading
fp = fopen("demo.txt", "r");
while ((c = getc(fp)) != EOF)
{ printf("%c", c);
}
fclose(fp);
return 0;
}
OTHER EXAMPLES
#include <stdio.h>
#include <stdlib.h>
int main()
{ int num;
FILE *fptr;
if ((fptr = fopen("C:\\program.txt","r")) == NULL)
{
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}
fscanf(fptr,"%d", &num);
printf("Value of n=%d", num);
fclose(fptr);
return 0;
}
FSEEK()
Syntax of fseek()
int fseek(FILE *pointer, long int offset, int position)
int main()
{
FILE *fp; Output:
fp = fopen("test.txt", "r");
81
return 0;
}
THANK YOU
ANY QUESTION ?