I want database management in C
To ADD,LIST,DELETE,INSERT using structures and files..........not in linked list.............. can anyone help me out....because this is the first time to do long and some what complex program for me

First define the structure you want to store in the file (database). Then begin writing the code to prompt for the information needed to fill in one structure member. When that is filled in save it to the file. Myself, I would use binary files because they are easier to write, read and search but the file format may or may not be specified in your program specifications.

Don't get overwhelmed by the complexity of the program. Break it down into small chunks, code and test one part at a time and pretty soon you will have the whole thing completed.

This is my program and i want INSERT code to rewrite properly.........because mine is not Working properly........

#include<stdio.h> 
#include<stdlib.h>
#include<string.h>
void menudriven()
{
        printf("\nENTER YOUR FOLLOWING CHOICE\n");
        printf("1 ==> ADD RECORD\n");
        printf("2 ==> LIST RECORD\n");
        printf("3 ==> INSERT RECORD\n");
        printf("0 ==> EXIT\n");
}
int main() 
{
        FILE *fp,*fp1;
        char another = 'y';
        int choice,ch,a;
        int line,count = 1;
        struct emp
        {
                char name[40];
                int age;
                float bs;
        };
        struct emp e;
        char empname[40];
        long int recsize,file,pos;
        fp = fopen("EMP","rb+");
        if(fp == NULL)
        {
                fp = fopen("EMP","wb+");
                if(fp==NULL)
                {
                        puts("CANNOT OPEN FILE");
                        exit(1);
                }
        }
        recsize = sizeof(e);
//        file = sizeof(fp);
        printf("recsize = %d\n",recsize);
//        printf("file = %d\n",file);
        while(1)
        {
                menudriven();
                printf("ENTER  YOUR CHOICE\n");
                scanf("%d",&choice);
                 switch(choice)
        {
                case 1:
                        fseek(fp,0,SEEK_END) ;
                        while(another == 'y')
                        {
                                printf("\n enter name,age and Basic Salary\n");
                                scanf("%s %d %f",e.name,&e.age,&e.bs);
                                fwrite(&e,recsize,1,fp);
                                printf("\n ADD ANOTHER RECORD (Y/N)");
                                while((ch = fgetc(stdin))!= EOF && ch != '\n');
                                scanf("%c",&another);

                        }
                        break;
                case 2:
                        rewind(fp);
                        while(fread(&e,recsize,1,fp)==1)
                        printf("\n %s %d %f",e.name,e.age,e.bs);
                        break;
                case 3:
                        while(another == 'y')
                        {
                    printf("\nEnter the line no. In which it has to be inserted = ");
                      scanf("%d",&line);
                                fp1  = fopen("TEMP1","wb");
                                rewind(fp);
                                while(fread(&e,recsize,1,fp) == 1)
                                {
                                   if(count==line)
                                   {
                                   printf("\n enter name,age and Basic Salary\n");
                                    scanf("%s %d %f",e.name,&e.age,&e.bs);
                                      fwrite(&e,recsize,1,fp1);
                                   }
                                        fseek(fp1,recsize,SEEK_CUR);
                                        printf("fp =  %s %d %f\n",e.name,e.age,e.bs);
                                        fwrite(&e,recsize,1,fp1);
                                        count++;
                                }
                                fclose(fp);
                                fclose(fp1);
                                 remove("EMP");
                                rename("TEMP1","EMP");
                                fp = fopen("EMP","rb+");
                                printf("Add another record(Y/N)");
                                while((ch = fgetc(stdin))!= EOF && ch != '\n');
                                scanf("%c",&another);
                        }
                        break;
                case 0:
                        fclose(fp);
                        exit(0);
                }
        }
}

My OUTPUT:

ENTER YOUR FOLLOWING CHOICE
1 ==> ADD RECORD
2 ==> LIST RECORD
3 ==> INSERT RECORD
0 ==> EXIT
ENTER  YOUR CHOICE
2

 A 20 1.000000
 B 21 2.000000
 C 22 3.000000
 D 23 4.000000
 E 24 5.000000
ENTER YOUR FOLLOWING CHOICE
1 ==> ADD RECORD
2 ==> LIST RECORD
3 ==> INSERT RECORD
0 ==> EXIT
ENTER  YOUR CHOICE
3
Enter the line no. In which it has to be inserted = 3
fp =  A 20 1.000000
fp =  B 21 2.000000

 enter name,age and Basic Salary
F
25
6
Add another record(Y/N)n

ENTER YOUR FOLLOWING CHOICE
1 ==> ADD RECORD
2 ==> LIST RECORD
3 ==> INSERT RECORD
0 ==> EXIT
ENTER  YOUR CHOICE
2

A 20 1.000000
B 21 2.000000
F 25 6.000000
F 25 6.000000      
C 22 3.000000
D 23 4.000000
E 24 5.000000

So why im getting 2 F statemnt .........where i made wrong in INSERT code

Edited 3 Years Ago by mike_2000_17: Fixed formatting

In the code for case 1, Your second record will over write the first one, third will over write the second record and so one.
What you want to do is keep a count of how many records have already been inserted and then each time the user wishes to enter a new record, use fseek to skip ahead in that file

This article has been dead for over six months. Start a new discussion instead.