1 / 13

Files

fread(),fwrite(),fgets(),fputs(),fgetc() and fputc()

Download Presentation

Files

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FILES fgetc(),fputc(),fgets(),fputs(),fread() and fwrite() functions

  2. 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.

  3. Text files are: fgets(),fputs(),fgetc() and fputc() Binary files are: fread() and fwrite()

  4. 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);

  5. Program for fputc(): #include <stdio.h> void main() { FILE *f; f = fopen("new.txt", "w"); fputc('a',f); fclose(f); }

  6. 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(); }

  7. 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);

  8. 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); }

  9. 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); }

  10. 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);

  11. 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; }

  12. 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; }

  13. THANK YOU

More Related