Struct Using C

Posted by Bl4ckB0y On 19.46 0 comments

Studebt Database
/*
this program using dev C++
you can apply this program to visual C++
it use to make symple record about data of student
you can develop it to make big program
by develop the functions and the add the struct
it is just for fun
*/
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "string.h"
#define MAX 20
#define MIN 11
/*Prototipe of struct*/
typedef struct
{
char name[MAX]; /*it is the part of struct student*/
char number[MIN];/*it is the part of struct student*/
}student;
/*this function to make new file an automatically open the file*/
void make_new(char name[20])
{
puts("Please Type the File Name with the extention :");/*it type messege to konsole
(DOS) */
gets(name);/*it insert the of file to name variabel*/
FILE *ptr; /*it is pointer to file*/
ptr=fopen(name,"wb"); /*pointer to file ptr point to file (name) (created the file)*/
fclose(ptr); /*pointer to file escape from point to file (name)*/
puts("File was created !!"); /*it type to messege to konsole (DOS) */
}
/*this functions to fill up the file*/
int fill_up(char name[20])
{
FILE *ptr; /*declarations of pointer*/
if((ptr=fopen(name,"rb+"))==NULL) /*pointer ptr point ot the file (name) using read
binary mode*/
{puts("File not Found ????"); /*it type to messege to konsole (DOS) */
return(0);} /*return to the main function*/
int k; /*it the sum of student in the file*/
k=getw(ptr); /*it take the sum of number in the file*/
printf("it found %d Record\n\n",k); /*it type the sum of student in konsole*/
if(k==-1) /*if the sum of student (k) is (-1) that meaning the file is empty*/
{k=0;fwrite(&k,sizeof(k),1,ptr);}
else{fclose(ptr);ptr=fopen(name,"ab+");} /*change the mode of operation into make,
write, and read*/
student study; /*it declare the anymous of struct student*/
int sum; /*it type to konsole about sum of student which added to file*/
printf("please insert Sum of Student : ");
scanf("%d",&sum); /*get the sum of student*/
fflush(stdin); /*clear the buffer*/
for(int j=0;sum>j;j++) /*iterate until number of sum of student which added to file*/
{
printf("Name : ");
gets(study.name); /*take the name of student*/
printf("Number: ");
gets(study.number); /*take the number of student*/
fwrite(&study,sizeof(study),1,ptr); /*write the name and number of student to
file*/
system("cls"); /*clear the console*/
}
fclose(ptr); /*close the file pointer*/
k=k+sum; /*sum of the new student added to the file*/
ptr=fopen(name,"rb+"); /*open the file again*/
fwrite(&k,sizeof(k),1,ptr); /*write the new sum of student to file*/
fclose(ptr); /*close the file pointer and return to the main*/
}
/*this function to read the data in the file*/
int read_data(char name[20])
{
student study; /*declare of the struct using initialize*/
FILE *ptr; /*file pointer*/
if((ptr=fopen(name,"rb"))==NULL) /*if file (name) not found function return to the
main*/
{puts("File not Found ?");return(0);}
int sum; /*it is sum of student in the file */
sum=getw(ptr); /*take integer data from file*/
printf("it %d Record\n\n",sum);
if(sum==-1) /*if the file is empty function return to the main*/
{puts("file is Empty???");return(0);}
for (int j=0;sum>j;j++) /*looping until sum of student*/
{
fread(&study,sizeof(study),1,ptr);
printf("%s\t%s\n",study.number,study.name);
}getch(); /*to stop until user press a key*/
system("cls"); /*clean the konsole screen*/
fclose(ptr); /*close the file pointer*/
}
/*this funtions to sort the data (record) by number*/
int sort_number(char name[20])
{
student temp ; /*it use for temporary variable using typedata struct*/
student study[10]; /*maksimum numberof student is 10, it is array of struct*/
FILE *ptr; /*file pinter*/
if((ptr=fopen(name,"rb"))==NULL) /*if ptr not found it return NULL */
{puts("file not Found");return(0);}
int sum; /*it is the sum of student in the file*/
sum=getw(ptr); /*get integer from file */
if(sum==-1) /*if the sum is (-1) the file must be Empty*/
{puts("file Empty ???");return(0);}
for (int j=0;sum>j;j++) /*load the data to the memory*/
{
fread(&temp,sizeof(temp),1,ptr); /*read data from the file*/
for(int i=0;MAX>i;i++)
{
study[j].name[i]=temp.name[i] ; /*load (name) to real struct */
}
for(int i=0;MIN>i;i++)
{
study[j].number[i]=temp.number[i]; /*load (number) to real struct*/
}
}
/*Sorting Process*/
/*it using bublesor,t you can use other sorting methode*/
for(int j=0;sum>j;j++)
{
for(int i=j+1;sum>i;i++)
{
int y=strcmp(study[j].number,study[i].number); /*compare the number*/
if(y==1)
{ /*Swaping system*/
/*because C cannot load from any array element to the another array
element*/
/*we must load it one by one*/
/*First step*/
/*load the j element to temporary element*/
for(int u=0;MAX>u;u++)
{
temp.name[u]=study[j].name[u];
}
/*the number must load too*/
for(int u=0;MIN>u;u++)
{
temp.number[u]=study[j].number[u];
}
/*second*/
/*load the i element to j element*/
for(int u=0;MAX>u;u++)
{
study[j].name[u]=study[i].name[u];
}
/*the number must load too*/
for(int u=0;MIN>u;u++)
{
study[j].number[u]=study[i].number[u];
}
/*third*/
/*load the temporary element to the i element*/
for(int u=0;MAX>u;u++)
{
study[i].name[u]=temp.name[u];
}
/*the number must load too*/
for(int u=0;MIN>u;u++)
{
study[i].number[u]=temp.number[u];
}
/*after this the element from j can swap to i, and i can swap to j*/
}
}
}
/*after all element was done to sort now we can print it to the screen*/
for(int k=0;sum>k;k++)
{
printf("%s\t%s\n",study[k].number,study[k].name);
}
fclose(ptr);
}
/*this function like the sort_number but we compare the name to sort the file*/
int sort_name(char name[20])
{
student temp;
student study[10];
FILE *ptr;
if((ptr=fopen(name,"rb"))==NULL)
{puts("file not found ?");return(0);}
int sum;
sum=getw(ptr);
printf("it found %d Record\n\n",sum);
if(sum==-1)
{puts("empty file ???");return(0);}
for (int j=0;sum>j;j++)
{
fread(&temp,sizeof(temp),1,ptr);
for(int i=0;MAX>i;i++)
{
study[j].name[i]=temp.name[i];
}
for(int i=0;MIN>i;i++)
{
study[j].number[i]=temp.number[i];
}
}
/*massage*/
for(int j=0;sum>j;j++)
{
for(int i=j+1;sum>i;i++)
{
int y=strcmp(study[j].name,study[i].name);
if(y==1)
{
/*first*/
for(int u=0;MAX>u;u++)
{
temp.name[u]=study[j].name[u];
}
for(int u=0;MIN>u;u++)
{
temp.number[u]=study[j].number[u];
}
/*second*/
for(int u=0;MAX>u;u++)
{
study[j].name[u]=study[i].name[u];
}
for(int u=0;MIN>u;u++)
{
study[j].number[u]=study[i].number[u];
}
/*third*/
for(int u=0;MAX>u;u++)
{
study[i].name[u]=temp.name[u];
}
for(int u=0;MIN>u;u++)
{
study[i].number[u]=temp.number[u];
}
}
}
}
for(int k=0;sum>k;k++)
{
printf("%s\t%s\n",study[k].number,study[k].name);
}
fclose(ptr);
}
/*thhis funtion to delete the record*/
int delete_data(char name[20])
{
student study; /*declare variabel study using type student (struct)*/
FILE *ptr;
if((ptr=fopen(name,"rb+"))==NULL)
{puts("file not found ?");return(0);}
int sum;
sum=getw(ptr);
printf("it found %d Record\n\n",sum);
if(sum==-1)
{puts("file is Empty ???");return(0);}
puts("file opened");
puts("Insert the Index : "); /*ask the number of index wan to be deleted*/
int y;scanf("%d",&y); /*take the number of index*/
if(y>sum){puts("index not found !!!");return(0);} /*if the index more big than sum,
return to teh main*/
fseek(ptr,sizeof(study)*(sum-1)+sizeof(int),SEEK_SET); /*slide pointer file to the last
element*/
fread(&study,sizeof(study),1,ptr); /*read the last element*/
fseek(ptr,sizeof(study)*(sum-y)+sizeof(int),SEEK_SET); /*slide the pointer too the
element which to be deleted*/
fwrite(&study,sizeof(study),1,ptr); /*write to the last element to the element which to
be deleted*/
sum--; /*sum decrement by 1*/
fseek(ptr,0,SEEK_SET);/*slide the pointer to the number of sum*/
fwrite(&sum,sizeof(sum),1,ptr);/*write the new sum in the file*/
fclose(ptr);/*close file*/
}
/*take the name of file*/
int open_file(char name[20])
{
puts("enter the file name and the extentions : ");
gets(name);
return(0);
}
/*it is the main function*/
main(void)
{char answer;
char name[20];
do
{
puts("type your choose: ");
puts("1.make new file");
puts("2.insert Data");
puts("3.sort by Name");
puts("4.sort by number");
puts("5.Delete Data");
puts("6.Read Data");
puts("7.Open File");
char choose;
choose=getch(); /*tkae the user choose*/
system("cls");
switch(choose) /*switch the user choose*/
{
case '1' : make_new(name);break;/*go to make new function*/
case '2' : fill_up(name);break;/*go to fill up function*/
case '3' : sort_name(name);break;/*go to sort by name function*/
case '4' : sort_number(name);break;/*go to sort by number function*/
case '5' : delete_data(name);break;/*go to delete funtion*/
case '6' : read_data(name);break;/*go to read data function*/
case '7' : open_file(name);break;/*go to oen file function*/
default : puts("that's wrong ??");
}
puts("press y for return!");
answer=getch();/*get the answer*/
}
while(answer=='y'||answer=='Y');/*repeat program if type yes (y/Y)*/
system("cls");
puts("Thank for use our software !!");
getch();
}

Categories:

0 Response for the "Struct Using C"

Posting Komentar

your Ads Here

Your Ad Here FellowEquality.com