Library Functions for Reading/Writing from/to a File
A. String Input/ Output
Using string i/O function, data can be read from a file or written to a file in the form of array of characters.
i. fgets():
It is used to read string from file.
fgets(string_ variable, int_ value, file_ ptr_ variable);
ii. fputs():
It is used to write a string to a file.
fputs(string, file_ ptr_ variable);
Example
Write a program to open the file “test.txt ” created , read its content and display to the screen.
#include<stdio.h> int main() { FILE *fptr; char s[100]; fptr=fopen ("c:\\test.txt","r"); if(fptr==NULL) { printf("\nFile can not be opend"); exit(1); } fgets(s, 100, fptr); printf("The text from file is:\t%s",s); fclose(fptr); getch(); return 0; } Output The text from file is: Welcome to my college
B. Character Input/ Output
Using character I/O functions, data can be read from a file or written to a file one character at a time.
i. fgetc():
It is used to read a character to read a character from a file.
char_ variable=fgetc(file_ ptr_ variable);
ii. fputc():
It is used to write a character to a file.
fputc(character or char_ variable, file_ ptr_ variable);
Example
Write a program to create file and write some text to it writing one character at a time using fputc() function. The program should write until hits enter key. Read filename from user.
#include<stdio.h> int main() { FILE *fptr; char c; char fileName [20]; printf("Enter the name of file"); scanf("%s", &fileName); fptr=fopen(fileName,"w"); if(fptr=NULL) { printf("File can not be created"); exit(0); } printf("Enter Text. Hit enter key to stop writing"); fflush(stdin); while(c=getchar())!='\n') { fputc(c.fptr); } fclose(fptr); return 0; } Output Enter the name of file: c:\student.dat Enter Text. Hit enter key to.stop writing This is example which reads some text from keyword character by character and stores in a file student.dat in c:\drive.
C. Formatted Input/ Output
These functions are used to read numbers, character or string from a file or write them to a file in format as our requirement.
i. fprintf():
It is formatted output function which is used to write integer , float, char or string data to a file.
fprintf(file_ ptr_ variable,"control_ string",&list_ variable);
ii. fscanf():
It is formatted input function which is used to read integer, float, char or string value from a file.
fscanf(file_ ptr_ variable, "controle_ string", &list_ variables);
Example
Write a program to create a file named student.txt in D:\ drive and write name, roll, address and marks of a student to the file.
#include<stdio.h> int main() { FILE *f; char name [20], address[20]; int roll; float marks; f=fopen("d:\student.txt","w"); if(f=NULL) { printf("File can not be created!!!"); exit(1); } printf("\Enter name of student :t"); gets(name); printf("Enter roll number"); scanf("%d",&roll); fflush(stdin); printf("Enter address of student "); gets(adress); printf("Enter marks of the student"); scanf("%f", &marks); printf("The data is being written in file....."); fprintf(f,"Name=%s\nRoll=%d\naddress=%s\nmarks=%.2f",name,roll,address,marks); fclose(f); getch(); return 0; } Output Enter name of student: Ram pd sharam Enter roll number: 1001 Enter address of student : Kathmandu Enter marks of the student: 89.5 The data is being written in file...........
Binary Data Files
The binary files organize data into blocks containing contiguous bytes of information. The binary files are recognized as a stream of bytes and programming languages tend to work with streams rather than files. In binary file, the opening mode of text mode file is appended by a character ‘b’ .
Example
Write a C program to write some text “welcome to my college” to a data file in binary mode. Read its content and display it.
#include<stdio.h> int main() { FILE *fptr; char c; fptr=fopen ("test.txt", "w+b"); if(fptr==NULL) { printf("File can not be created"); } fputs("Welcome to my College",fptr); rewind(fptr); printf("The content from file"); while((c=fgetc(fptr))!=EOF) { printf("%c",c); } fclose(fptr); return 0; } Ouptput The content from file: Welcome to my college