Let us create a simple File Security System using C programming file handling and other basic concepts.
About The Application
A File Security System is a console application that can be used to prevent unauthorized access to a file. It prompts the user to enter a password before he/she can access the particular file. The system, if the password entered is correct, opens the file in reading mode and displays the characters of the file on the screen. However, if the entered password is incorrect the system will display a message and immediately exit itself.
Concepts used
Functions
Functions are the building blocks for every C code. Every C Program has at least one main() function and other user-defined functions. There are two user-defined functions named check(), to verify whether the password entered is correct or not, and another one named open file(), which opens and displays file content on screen.
File Handling
C Programming Language offers various inbuilt file handling functions. These functions are present in the stdio.h header file. This application makes use of the following file functions:
- fopen() - used to open a particular file in specific mode ,and
- fclose() - used to close a particular file ,and
- fgetc() - used to read data from a file character-wise.
- fflush() - used to clear input buffer.
String Handling
String is a sequence or we can say an array of characters terminated by a ‘/0’(NULL) character. C Program offers various String Handling functions to facilitate programmers. These functions are present in the string.h Header file.
Here two main functions of string handling are being used,
- gets() - used to read a string from the keyboard
- strcmp() - used to compare two strings, takes two string arguments
Other Miscellaneous Functions
- exit() - used to terminate the program immediately. Present in stdlib.h header file
- Sleep() - used to pause program execution for a given time(in seconds). Present in windows.
Code
/*A Simple File Security System
Demo in C Language.
Author: Deepak Kumar
*/
#include<stdio.h> //Header File for I/O functions
#include<stdlib.h> //Header File for exit() function
#include<windows.h> //Header File for Sleep() function
#include<string.h> //Header File for String functions
FILE * fp; //This is File Pointer. It is a special type of pointer which assists us in
//handling files in C Programs.
char code[11], ch; //Global Variables Declaration. "code" will be used to get a password from the
//user using the keyboard whereas "ch" will be used to read data from a file.
char passcode[11] = "12345"; //"passcode" holds actual password for file named data.txt.
//openfile() is a user-defined function used to open and read file.
void openfile() {
fp = fopen("data.txt", "r"); //opening file in read mode.
if (fp == NULL) //checking for any errors in opening.
printf("\n\t\t\t\tFile can't be opened.\n");
else { //if no errors are there file will be opened .
printf("\n\t\t\t\tOpening File...wait\n");
// Printing what is written in file.
// character by character using loop.
do {
ch = fgetc(fp);
printf("%c", ch);
// Checking if character is not EOF.
// If it is EOF stop reading.
} while (ch != EOF);
// Closing the file
fclose(fp);
printf("\n\t\t\t\tFile has been closed.");
}
} //openfile() defination ends here
//check() is used to prompt for password and verify before opening file.
void check() {
printf("\n\t\t\t\tEnter File Password:\t");
gets(code); //takeing code as input from user
fflush(stdin); //fflush(stdin) is an inbuilt function used to clear input stream buffers.
if (strcmp(code, passcode) == 0)
//comparing user-entered password with actual password using strcmp().
openfile();
else {
printf("\n\t\t\t\tWrong Password...System is Closing for security concerns.");
Sleep(500); //Sleep() function is used to pause the execution for a given number of seconds.
exit(0); //exit(0) function is used to terminate the program immediately.
}
}
//Driver Program
//main() function starts from here
int main() {
check(); //caling check() function
return 0;
} //end of main()
Variable Description Table
Variable |
Datatype |
Purpose |
ch |
char |
To read Characters from a file. |
fp |
FILE type |
To Perform File Operations. |
code |
char array(string) |
To Take code as input from the user. |
passcode |
char array(string) |
To Store the actual password. |
Snapshots
Summary
As we saw that using simple concepts of C Language we can design a basic console application, C also offers us a rich library of functions for application development. It provides us with tools for creating a skeleton, basic functioning, and user interaction for our application.