Pages

Saturday, August 1, 2020

C LANGUAGE PROJECT



Basic MEDICAL STORE - PROJECT

/* ------- Files details -------
1. Stock_details.dat (File used to store stock item details)
2. Sales_details.dat (File used to store sales details)
*/

#include<stdio.h>
#include<conio.h>
#include<process.h>

//Function declarations
void main_menu();
void insert_record();
void sales_details();
void sales_report();
void stock_details();

// Definition of main function
void main()
{
clrscr();
int c;
char ch;
do
{
main_menu();

printf("\n\nEnter your choice (1-5):");
scanf("%d",&c);

// Calling funtion by switch statement
switch(c)
{
case 1:
insert_record(); break;
case 2:
sales_details(); break;
case 3:
sales_report(); break;
case 4:
stock_details(); break;
case 5:
exit(0);
default:
printf("Wrong choice, try again");
}

printf("\n\n\n\nDo you want to continue (y/n):");
ch=getche();
}while(ch=='y' || ch=='Y');
getch();
}

//Define menu details funtcion
void main_menu ()
{
clrscr();
printf("\n\t\t\t---------------------------\n");
printf("\t\t\t* * * Bhavesh Medico * * *");
printf("\n\t\t\t---------------------------\n");

printf("\n\n\n----- Insert Records -----\n");
printf("1. New Medicine");
printf("\n2. New Sales");

printf("\n\n\n----- Reports -----\n");
printf("3. Sales");
printf("\n4. Stock");

printf("\n\n\n----- Exit Function -----\n");
printf("5. Exit");
}

//Define menu details funtcion
void insert_record()
{
int m_id,price;
char m_name[20], brand[40];
char m_date[20], e_date[40];

printf("Enter medicine no:");
scanf("%d",&m_id);
printf("Enter medicine  name:");
scanf("%s",m_name);
printf("Enter company/brand name:");
scanf("%s",brand);
printf("Enter price:");
scanf("%d",&price);
printf("Enter manufacturing date:");
scanf("%s",m_date);
printf("Enter expiry date:");
scanf("%s",e_date);

FILE *ob;
ob=fopen("stock_details.dat","a");
fprintf(ob,"\n%d",m_id);
fprintf(ob,"\t%s",m_name);
fprintf(ob,"\t%s",brand);
fprintf(ob,"\t%d",price);
fprintf(ob,"\t%s",m_date);
fprintf(ob,"\t%s",e_date);
fclose(ob);

}

//Define menu details funtcion
void sales_details()
{
int price,q;
char date[20], brand[20];
char item_name[20], buyer_name[40];

printf("\nAvailable stocks.....\n");
stock_details();
printf("Enter date (MM/DD):");
scanf("%s",&date);
printf("Enter customer name:");
scanf("%s",&buyer_name);
printf("Enter item name:");
scanf("%s",&item_name);
printf("Enter item price:");
scanf("%d",&price);
printf("Enter quantity:");
scanf("%d",&q);

FILE *ob;
ob=fopen("sales_details.dat","a");
fprintf(ob,"\n%s",date);
fprintf(ob,"\t%s",buyer_name);
fprintf(ob,"\t\t%s",item_name);
fprintf(ob,"\t\t  %d",q);
fprintf(ob,"\t\t%d",(q*price));

fclose(ob);
}

//Define menu details funtcion
void sales_report()
{
char ch;
FILE *ob;
ob=fopen("sales_details.dat","r");
if(ob==NULL)
{
printf("No Records found");
}
else
{
printf("\n---------------------------------------------------------------");
printf("\nDate\tCust.Name\tItem name\tQuant.\t     Total price");
printf("\n---------------------------------------------------------------\n");

while((ch=fgetc(ob))!=EOF)
{
printf("%c",ch);
}
fclose(ob);
}
}

//Define menu details funtcion
void stock_details()
{
char ch;
FILE *ob;
ob=fopen("stock_details.dat","r");
if(ob==NULL)
{
printf("No Record found");
}

printf("\n----------------------------------------------------------");
printf("\nCode \tName \tBrand \tPrice \tManuf.Date \tExp.Date");
printf("\n----------------------------------------------------------\n");

while((ch=fgetc(ob))!=EOF)
{
printf("%c",ch);
}
printf("\n----------------------------------------------------------\n");

fclose(ob);
}


FILE READING IN C



File reading is used to fetch the data from an already existing file and print the file''s data in the console window (output window)

PROGRAM TO READ/FETCH DATA FROM FILE
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();

// Create FILE class pointer 
FILE *fp=fopen("test.txt","r");
int ch=getc(fp);

// Loop to read data from file
while(ch != EOF)
{
putchar(ch); // Print data on output (console) window
ch = getc(fp); //Read data by ch variable
}
getch();
}


----------------------------

         OUTPUT 
----------------------------
Welcome in my first file


 C LANGUAGE PROJECTS >>

FILE WRITING IN C



The file writing feature allows programmers to write different types of data and information in a file.

SYNTAX TO CREATE AND OPEN A NEW FILE
file_pointer_name(file_pointer_name,"writing mode");

EXAMPLE
FILE *fp;
fp=fopen("File_name", "w");

Example Explanation: In the above example, FILE is a predefined file class in the C programming language.

 *fp is a special pointer of class 'FILE'

fopen is a predefined function of file handling function in C programming language and it is used to open an already existing file (if file does not exist then it create file first) 

File_name is a file, that you want to open.

w stands for writing mode.


PROGRAM TO WRITE A FILE IN C
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
FILE *fp;  //Create a file pointer

// Open file in writing mode 
fp=fopen("test.txt","w");

//Wring content in "test.txt" file
fprintf(fp,"Welcome in first file");

printf("Data written in file successfully ...");
getch();
}




----------------------------

         OUTPUT 
----------------------------

Data written in file successfully ...


HOW TO SHOW FILE CONTENTS

We can read file contents by File reading features.
We can also see the file contents by using the MS-DOS commands.

After successfully run the C program, 
Follow the below steps to show the file contents.

1. Click on the "File" menu and then click on "DOS shell"


2. Now type the following command.
type test.txt


WRITE FILE IN APPEND




We can write a file in the following two modes.
1. Write mode
2. Append mode

Write (w) mode and Append (a) mode, both are used to write a file. The only difference between write and append mode is when you open a file in write mode the file reset and this reset process delete all the previous data of the file, while in Append mode, the cursor is positioned at the end of the last line in the file therefore new data will add without disturb the previous data of the file.

PROGRAM TO WRITE FILE IN APPEND MODE
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
FILE *fp;  //Create a file pointer

// Open file in writing mode 
fp=fopen("test.txt","a");

//Wring content in "test.txt" file
fprintf(fp,"Welcome in first file");

printf("Data written in file successfully ...");
getch();
}



----------------------------

         OUTPUT 
----------------------------

Data written in file successfully ...

FILE HANDLING FUNCTIONS IN C


File handling functions are used to perform different types of tasks and there are different types of file handling functions in C programming language, which mentioned below.


FILE FUNCTION
DESCRIPTION
fopen()
To create a new file or open an existing file
fclose()
To closes a file
getc()
To reads a character from a file
putc()
To writes a character to a file
fscanf()
To reads a set of data from a file
fprintf()
To writes a set of data to a file
getw()
To reads a integer from a file
putw()
To writes a integer to a file
fseek()
To set the position to desire point
ftell()
To gives current position in the file
rewind()
To set the position to the beginning point


FILE WRITING IN C >>

FILE MODES IN C



In C language, file modes are used to specifies the purpose for opening a file.

There are different types of file modes, which are described below. 

FILE MODE
DESCRIPTION
r
To opens a text file in reading mode
w
To opens or create a text file in writing mode.
a
To opens a text file in append mode
r+
To opens a text file in both reading and writing mode
w+
To opens a text file in both reading and writing mode
a+
To opens a text file in both reading and writing mode
rb
To opens a binary file in reading mode
wb
To opens or create a binary file in writing mode
ab
To opens a binary file in append mode
rb+
To opens a binary file in both reading and writing mode
wb+
To opens a binary file in both reading and writing mode
ab+
To opens a binary file in both reading and writing mode