C Programming

12-File Handling in C Programming

C Programming Lessons, Learn C Programming, Introduction to C Language, How to Program with C

File Handling in C, C Programming, Read and Write Data to Files

What are the files used for?

It is used to store data after the program is terminated. A file is a collection of related records.

The fopen() function is used to open a file and the fclose() function to close it.

The fgetc function, like getchar, reads a character from the file.

The fgetc(stdin) call reads a character from stdin, i.e. standard input. This call is equivalent to getchar().

The fputc function, as an argument, takes a character to print and a pointer to the file in which the character will be printed.

The call fputc(‘a’,stdout) prints the character ‘a’ to stdout i.e. standard output. This call is equivalent to putchar(‘a’).

FunctionIts Task
fputcsaves one character data to file
fputssaves a string of characters to the file  
fwritesaves a record, string, or character to the file as binary
fprintsaves formatted data to file
fgetcreads one character data to file
fgetsreads a string of characters from the file
freadreads a record, string, or character into a file in binary
fscanfreads the data in the file by formatting it

When working with files, pointer definition must be made.

FILE *fPtr;

The syntax structure required to open a file is as follows;

fPtr = fopen(“fileopen”,”mode”);

Let’s take a look at these mods mentioned

MOD 
ropen a file for reading
wcreate a file for writing
aadd; to append to the end of a file
r+open a file to update (read and write)
w+create a file to update. If the file already exists, its previous content is deleted.
a+add; open or create a file to update, writing is done to the end of the file.
FILE Mods

*A file that needs to be opened in update mode (r+), opening it in write mode (w+) will delete all the contents of the file

Example 1

#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *file_1;
file_1 = fopen("trial.txt", "w");
fprintf(file_1, "Hello World!! ");
fclose(file_1);
return 0;
}
Creating a .txt File 
File Handling in C Programming
Creating a .txt File

Example 2 : Storing Data in a File

#include <stdio.h>
#include <stdlib.h>
int main()
{
int account;
char name[30];
double balance;
FILE *cfPtr; //cfPtr = customer.dat file pointer
if ((cfPtr=fopen("customer.dat", "w"))==NULL)
printf("file couldn't open");
else
{
printf("Enter account number, Name and Balance: \n");
printf("Enter EOF character to end \n");
printf("?");
scanf("%d %s %lf", &account, name, &balance); //writes the data to the file
while(!feof(stdin))
{
fprintf(cfPtr,"%d %s %.2lf\n",account,name,balance);
printf("?");
scanf("%d %s %lf", &account, name, &balance);
}
fclose(cfPtr);
}
return 0;
}
Storing Some Data in a File 
File Handling in C Programming
Storing Some Data in a File

Example: Read Data from a File

#include<stdio.h>
#include <stdlib.h> // for exit() function
int main()
{
char c[1000];
FILE *fptr;
if ((fptr = fopen("info.txt", "r")) == NULL)
{
printf("Error! opening file");
// exit from program if file pointer returns NULL.
exit(1);
}
// read the text until newline
fscanf(fptr,"%[^\n]", c);
printf("Data from the file:\n%s", c);
fclose(fptr);
return 0;
}
Reading Data From a File 
File Handling in C Programming
Reading Data From a File

Leave a Reply

Your email address will not be published. Required fields are marked *