E N D
FILES fgetc(),fputc(),fgets(),fputs(),fread() and fwrite() functions
Types of Files in C Generally, a text file contains alphabets, digits, and special characters or symbols, while a binary file contains bytes or a compiled version of the text. It is important to recognize two types of files when dealing with files: Text Files Binary Files Text Files: Text files contain data in the form of ASCII characters and are generally used to store a stream of characters. Each line in a text file ends with a new line character (‘/n’). Text files are used to store the source code. Binary Files: Binary files contain data that is stored in a similar manner to how it is stored in the main memory. Instead of ASCII characters, it is stored in binary format. The binary files can be created only from within a program and their contents can only be read by a program.
Text files are: fgets(),fputs(),fgetc() and fputc() Binary files are: fread() and fwrite()
Text files: Reading and writing characters using fgetc() and fputc() function fgetc(): The function fgetc() is used to read the character from the file. It returns the character pointed by file pointer, if successful otherwise, returns EOF. syntax:char ch; ch= fgetc(FILE *stream); or ch=fgetc(fp); fputc():The function fputc() is used to write the character to the file. It writes the character to the file, if successful otherwise, returns EOF. syntax: char ch; ch= fputc(char ch,FILE *stream); or ch=fputc(ch,fp);
Program for fputc(): #include <stdio.h> void main() { FILE *f; f = fopen("new.txt", "w"); fputc('a',f); fclose(f); }
Program for fgetc(): #include<stdio.h> #include<conio.h> void main() { FILE *f; char s; clrscr(); f=fopen("new.txt","r"); while((s=fgetc(f))!=EOF) { printf("%c",s); } fclose(f); getch(); }
Reading and writing string using fgetc() and fputc() function fgets(): To read a string (line of characters) from a file in C, fgets() function is used. syntax: fgets(char str[], int n, FILE *stream) Example: fgets(str,200,fp); fputs():To write a string (line of characters) into a file in C, fputs() function is used. syntax: char ch; ch= fputs(char str[],FILE *stream);
Program for fgets(): #include <stdio.h> void main() { FILE *f; f = fopen("file.txt", "w"); fputs("Reading data from a file is a common feature of file handling..",f); fclose(f); char arr[100]; f = fopen("file.txt","r"); printf("%s",fgets(arr,65,f)); fclose(f); }
Program for fputs(): #include <stdio.h> void main() { FILE *f; f = fopen("file.txt", "w"); fputs("Reading data from a file is a common feature of file handling..",f); printf ("Data Successfully Written to the file!"); fclose(f); }
Binary files: Reading and writing string using fgetc() and fputc() function fwrite():The fwrite() function is used to write records (sequence of bytes) to the file.A record may be an array or a structure. syntax: size_t fwrite(const void *ptr, size_t size, size_t n, FILE *fp); fread():The fread() function is the complementary of fwrite() function. fread() function is commonly used to read binary data. It accepts the same arguments as fwrite() function does. syntax: size_t fread(void *ptr, size_t size, size_t n, FILE *fp);
Program for fwrite(): #include<stdio.h> #include<stdlib.h> struct employee { char name[50]; char designation[50]; int age; float salary } employee; int main() { int n, i, chars; FILE *fp; fp = fopen("employee.txt", "wb"); if(fp == NULL) { printf("Error opening file\n"); exit(1); } printf("Testing fwrite() function: \n\n"); printf("Enter the number of records you want to enter: "); scanf("%d", &n); for(i = 0; i < n; i++) { printf("\nEnter details of employee %d \n", i + 1); fflush(stdin); printf("Name: "); gets(employee.name); printf("Designation: "); gets(employee.designation); printf("Age: "); scanf("%d", &employee.age); printf("Salary: "); scanf("%f", &employee.salary); chars = fwrite(&employee, sizeof(employee), 1, fp); printf("Number of items written to the file: %d\n", chars); } fclose(fp); return 0; }
Program for fread(): #include<stdio.h> #include<stdlib.h> struct employee { char name[50]; char designation[50]; int age; float salary } emp; int main() { FILE *fp; fp = fopen("employee.txt", "rb"); if(fp == NULL) { printf("Error opening file\n"); exit(1); } printf("Testing fread() function: \n\n"); while( fread(&emp, sizeof(emp), 1, fp) == 1 ) { printf("Name: %s \n", emp.name); printf("Designation: %s \n", emp.designation); printf("Age: %d \n", emp.age); printf("Salary: %.2f \n\n", emp.salary); } fclose(fp); return 0; }