I am new in c and dont know how to make a database in it
i know that there is no database connection with c but i want to make project on employee data using file processing like this
1. add (employee name, age, contact number)
2. edit (employee name, age, contact number)
3. delete
4. search
5. list
6. import/export

I am using dev c++ version 4.9.9.2

I have knowledge of read n write file but i dont know how this project will be code using file processing

can any one give me the instructions of how to make it

i know that there is no database connection with c

Not standard C, but there are libraries for pretty much every database package out there. If you're planning on writing your own database rather than connecting to a database package, here are a few thoughts to get you started based on your list of required tasks:

  • add: When adding, keep in mind the cost of searching, because finding a record quickly is key in editing, deleting, and (obviously) searching. This means that you shouldn't just append new records to a file, there should be some kind of method to the madness of how you organize both the files and contents of each file.

    Also note that if the database is to be scalable, you'll want a structure that supports breaking records up into multiple files instead of just one monolithic file. However, if it's just an ad hoc database that won't even be complex enough to justify something like MS-Access, you can probably get away with just a single flat file. Then the question of editing, deletion, and such become "how do I do xxxx with a file?", which is quite a bit simpler.

  • edit: Take note that unless you're using fixed length fields for records, there's the problem of size variance. If the new data is smaller or larger than the old data during an edit, you need some way of shrinking and growing the file.

    An alternative is to store one record per file, that way it's easier to change the size without affecting other records. But depending on your design, handling a mass of files for each record can be problematic.

  • delete: Deleting holds the same issues as editing where the new record size is smaller than the old record size. If you use a record per file, just delete the file, otherwise you need to remove the record.

    One option is to simply mark the record as deleted and then ignore it in all future operations with the possible exception of addition if you want to reuse the slot. If you don't reuse the slot then you'll still want to include some kind of cleanup operation that removes deleted records.

  • search: Depending on how many files you're using, this could be as simple as searching within a single file or as complex as using references in an index file to guide your search in the file system.
  • list: This is a variant of search where multiple records matching given criteria are returned. While I wouldn't recommend trying to duplicate the power of SQL, some basic stuff like finding N records in an age range would be a good idea.
  • import/export: I'm not entirely sure what you're thinking of with this.

Based on the information you've provided, the easiest "database" for your data is a standard text file. Each line in the file contains employee name, age, contact number in a fixed format, say
char name[20];
int age;
char contact[10];

Read the entire file into a structure array and process with your commands.

thanks to Narue and WaltP for help

M following ur intructions and if i found errors or problems then again I ll again ask for your help

for this should i use the .bin file or .txt file

our teacher told that use .bin file that it ll be used for these operations on file like edit or delete

you can use edit or delete operations on either the .bin file or .txt file as these only depend on your code

Since your teacher told you to use a binary file then your free to use a .bin file if it's a requirement

It doesn't really matter which you use to make your program work

the only difference is when you try to open, you can't read a binary file while your free to see your output in a text file

i have done the add, edit n delete part but m stuck in import n export
in import I have to import data from any file(program will ask for filename)in (.csv format) to my file
in export I have to save my data in a file with .csv format

how i can do it?

Why not just store the file as CSV? Then export is a simple file copy, and import is a copy after verifying that the import file is in the correct format.

Narue can u please explain me in detail
m not getting u

Narue can u please explain me in detail

I don't think I can be any more clear. Use CSV to store your database records. This removes the need to convert the database between some ad hoc internal format and CSV for the import/export operations. You can still use a .txt or .bin extension with CSV content.

CSV format is pretty simple -- each record is separated by '\n', as it is in almost every normal text file. Each field is separated by either a comma ',' or a tab '\t'. Which one you use is up to you and might depend on the data to be stored, and you can't mix the two. Text strings are surrounded with quotes ", while numeric data is not surrounded with anything

Example: "Once upon a time",1,23.45,"there were three little pigs"

ok i got what u said

now m working on the decoration of my project
i changed background color and text colour easily but cannot change the font size
can u please tell me how i can do this

here is my code

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdlib.h>
#include<dos.h>
 
 
 
 
struct contact
{
long ph;
char name[20],add[20],email[30];
}list;
 
char query[20],name[20];
FILE *fp, *ft;
int i,n,ch,l,found;
 
int main()
{

main:
system("cls");    /* ************Main menu ***********************  */
system("color 3");
system("color f3");
system("fontsize 24");
printf("\n\n\n\t\t\tMAIN MENU\n\t\t=====================\n\t\t[1] Add a new Contact\n\t\t[2] List all Contacts\n\t\t[3] Search for contact\n\t\t[4] Edit a Contact\n\t\t[5] Delete a Contact\n\t\t[0] Exit\n\t\t=================\n\t\t");
printf("Enter the choice:");
scanf("%d",&ch);
 
switch(ch)
{
case 0:
printf("\n\n\t\tAre you sure u want to exit?");
break;
/* *********************add new contacts************  */
case 1:
 
system("cls");
fp=fopen("contact.dll","a");
for (;;)
{ fflush(stdin);
printf("To exit enter blank space in the name input\nName (Use identical):");
scanf("%[^\n]",&list.name);
if(stricmp(list.name,"")==0 || stricmp(list.name," ")==0)
break;
fflush(stdin);
printf("Phone:");
scanf("%ld",&list.ph);
fflush(stdin);
printf("address:");
scanf("%[^\n]",&list.add);
fflush(stdin);
printf("email address:");
gets(list.email);
printf("\n");
fwrite(&list,sizeof(list),1,fp);
}
fclose(fp);
break;
 
/* *********************list of contacts*************************  */
case 2:
system("cls");
printf("\n\t\t================================\n\t\t\tLIST OF CONTACTS\n\t\t============================\n\nName\t\tPhone No\t    Address\t\tE-mail ad.\n====\n\n");
 
for(i=97;i<=122;i=i+1)
{
 
fp=fopen("contact.dll","r");
fflush(stdin);
found=0;
while(fread(&list,sizeof(list),1,fp)==1)
{
if(list.name[0]==i || list.name[0]==i-32)
{
printf("\nName\t: %s\nPhone\t: %ld\nAddress\t: %s\nEmail\t: %s\n",list.name, 
list.ph,list.add,list.email);
found++;
}
}
if(found!=0)
{printf("================================= [%c]- (%d)\n\n",i-32,found);
getch();}
fclose(fp);
 
}
 
break;
 
 
 
/* *******************search contacts**********************  */
case 3:
system("cls");
do
{
found=0;
printf("\n\n\t..::CONTACT SEARCH\n\t===========================\n\t..::Name of contact to search: ");
fflush(stdin);
scanf("%[^\n]",&query);
l=strlen(query);
fp=fopen("contact.dll","r");
 
system("cls");
printf("\n\n..::Search result for '%s' \n===================================================\n",query);
while(fread(&list,sizeof(list),1,fp)==1)
{
for(i=0;i<=l;i++)
name[i]=list.name[i];
name[l]='\0';
if(stricmp(name,query)==0)
{
printf("\n..::Name\t: %s\n..::Phone\t: %ld\n..::Address\t: %s\n..::Email\t:%s\n",list.name,list.ph,list.add,list.email);
found++;
if (found%4==0)
{
printf("..::Press any key to continue...");
getch();
}
}
}
 
if(found==0)
printf("\n..::No match found!");
else
printf("\n..::%d match(s) found!",found);
fclose(fp);
printf("\n ..::Try again?\n\n\t[1] Yes\t\t[0] No\n\t");
scanf("%d",&ch);
}while(ch==1);
break;
 
 
/* *********************edit contacts************************/
case 4:
system("cls");
fp=fopen("contact.dll","r");
ft=fopen("temp.dat","w");
fflush(stdin);
printf("..::Edit contact\n===============================\n\n\t..::Enter the name of contact to edit:");
scanf("%[^\n]",name);
while(fread(&list,sizeof(list),1,fp)==1)
{
if(stricmp(name,list.name)!=0)
fwrite(&list,sizeof(list),1,ft);
}
fflush(stdin);
printf("\n\n..::Editing '%s'\n\n",name);
printf("..::Name(Use identical):");
scanf("%[^\n]",&list.name);
fflush(stdin);
printf("..::Phone:");
scanf("%ld",&list.ph);
fflush(stdin);
printf("..::address:");
scanf("%[^\n]",&list.add);
fflush(stdin);
printf("..::email address:");
gets(list.email);
printf("\n");
fwrite(&list,sizeof(list),1,ft);
fclose(fp);
fclose(ft);
remove("contact.dll");
rename("temp.dat","contact.dll");
break;
 
 
/* ********************delete contacts**********************/
case 5:
system("cls");
fflush(stdin);
printf("\n\n\t..::DELETE A CONTACT\n\t==========================\n\t..::Enter the name of contact to delete:");
scanf("%[^\n]",&name);
fp=fopen("contact.dll","r");
ft=fopen("temp.dat","w");
while(fread(&list,sizeof(list),1,fp)!=0)
if (stricmp(name,list.name)!=0)
fwrite(&list,sizeof(list),1,ft);
fclose(fp);
fclose(ft);
remove("contact.dll");
rename("temp.dat","contact.dll");
break;
 
default:
printf("Invalid choice");
break;
}
 
 
printf("\n\n\n..::Enter the Choice:\n\n\t[1] Main Menu\t\t[0] Exit\n");
scanf("%d",&ch);
switch (ch)
{
case 1:
goto main;
 
 
case 0:
break;
 
default:
printf("Invalid choice");
break;
}
 
return 0;
}

font size can not be changed in console programs. You have to write a GUI program to do that.

what is that GUI program?

A Graphical User Interface program, i.e., a typical Windows program, as opposed to one in a text-only console window. GUI programs are more flexible, but a lot harder to write, which is why most introductory courses stick to console applications.

okay so i think there is no need to change font size now

can anyone tell me how i can edit records in the given code

/*
  This solution uses a PhoneRecord structure with the name and number stored in
  arrays with a fixed size. This allows the file operations to be very simple.
  You can just read or write a PhoneRecord structure since its size is fixed.

  If you wanted to allocate space for the name and number dynamically, then
  you would have to explicitly write the name and number strings as well as the
  PhoneRecord structure to the file. You would also need to include the length of each
  data item in the file so you knew how much to read back.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include<process.h>
#include<dos.h>

#define FIRST_NAME_LENGTH  31
#define SECOND_NAME_LENGTH 51
#define ADDRESS_LENGTH     71
#define EMAIL_LENGTH       31
#define NUMBER_LENGTH      21
#define TRUE                1
#define FALSE               0

/* Structure defining a name */
struct Name
{
  char firstname[FIRST_NAME_LENGTH];
  char secondname[SECOND_NAME_LENGTH];
  char address[ADDRESS_LENGTH];
  char email[EMAIL_LENGTH];
};

/* Structure defining a phone record */
struct PhoneRecord
{
  struct Name name;
  char number[NUMBER_LENGTH];
};

/* Function prototypes */
struct PhoneRecord read_phonerecord();              /* Read a name and number from the keyboard     */
struct Name read_name();                            /* Read a name from the keyboard                */
void show_record(struct PhoneRecord record);        /* Output name and number from a phone record   */
struct PhoneRecord find_numbers(struct Name name);  /* Find numbers corresponding to a given name   */
void add_record();  
void edit_records(struct Name name);                                 /* Add a new name and number                    */
void delete_records(struct Name name);              /* Delete records for a given name              */
void list_records();                                /* List all the records in the file             */
void show_operations();                             /* Displays operations supported by the program */
int equals(struct Name name1, struct Name name2);   /* Compare two names for equality               */

/* Global variables */
char firstname[FIRST_NAME_LENGTH];
FILE *pFile = NULL;   
FILE *ft;                              /* File pointer                           */
char *filename = "D:\\records.bin";                 /* Name of the file holding the records   */
char answer = 'n';                                  /* Stores input responses                 */


int main()
{
    system("cls");
   // system("color 5");
system("color f3");
  show_operations();                                /* Display the available operations */     

  for(;;)
  {
    printf("\n  Enter a letter to perform the function: ");
    scanf(" %c", &answer);
    switch(answer)
    {
      case 'A':                                     /* Add a new name and number record  */
        add_record();
        break;
        case 'E':                                     /* Add a new name and number record  */
        edit_records(read_name());
        break;
      case 'D':                                     /* Delete records for a given name   */
        delete_records(read_name());
        break;
      case 'F':                                     /* Find the numbers for a given name */
        find_numbers(read_name());
        break;
      case 'L':                                     /* List all the name/number records  */
        list_records();
        break;
      case 'Q':                                     /* Quit the program                 */
        return 0;
      default:
        printf("\nInvalid selection try again.");
        show_operations();
        break;
    }
  }
 }

/* Reads a name and number from the keyboard and creates a PhoneRecord structure */
struct PhoneRecord read_phonerecord()
{
  struct PhoneRecord record;    
  record.name = read_name();
  printf("Enter the number: ");
  scanf(" %[ 0123456789]",record.number);  /* Read the number - including spaces */
  return record;
}

/* Outputs the name and number from a phone record */
void show_record(struct PhoneRecord record)
{
     fflush(stdin);
     struct Name name;
  printf("\nFirst Name: %s \nSecond Name: %s   \nAddress: %s \nEmail: %s  \nContact Number: %s\n\n", record.name.firstname,record.name.secondname,record.name.address,record.name.email,record.number);
}

/* Add a new name and number */
void add_record()
{
  struct PhoneRecord record;

  if((pFile = fopen(filename, "a+")) == NULL)  /* Open/create file to be written in append mode */
  {
    printf("Error opening %s for writing. Program terminated.", filename);
    abort();
  }
  record = read_phonerecord();                 /* Read the name and number */  
  fwrite(&record, sizeof record, 1, pFile);
  fclose(pFile);                               /* Close file               */ 
  printf("\nNew record added.");
}

/* Read a name from the keyboard and create a Name structure for it */
struct Name read_name()
{
  struct Name name;
  fflush(stdin);
    printf("\nEnter a first name: ");
    scanf(" %s", &name.firstname);
    printf("Enter a second name: ");
    scanf(" %s", &name.secondname);
     printf("Enter Address: ");
   //  fgets(name.address,ADDRESS_LENGTH,pFile);
     //gets(address);
     scanf(" %[ ,.#-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789]",name.address);
fflush(stdin);
     printf("Enter Email: ");
    scanf(" %s", &name.email);
  return name;
}



// Edit Function
//new function

/*void edit_record(struct Name name)
{
     struct PhoneRecord record;
    // struct Name name;
     ft=fopen("temp.dat","w");
  fflush(stdin);
printf("..::Edit contact\n===============================\n\n\t..::Enter Firstname of contact to edit:");
scanf("%s",firstname);
while(fread(&name,sizeof(name),1,pFile)==1)
{
if(stricmp(firstname,name.firstname)!=0)
fwrite(&name,sizeof(name),1,ft);
}
fflush(stdin);
printf("\n\n..::Editing '%s'\n\n",name);
printf("\nEnter a first name: ");
    scanf(" %s", &name.firstname);
    printf("Enter a second name: ");
    scanf(" %s", &name.secondname);
     printf("Enter Address: ");
   //  fgets(name.address,ADDRESS_LENGTH,pFile);
     //gets(address);
     scanf(" %[ ,.#-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789]",name.address);
fflush(stdin);
     printf("Enter Email: ");
    scanf(" %s", &name.email);
printf("\n");
fwrite(&name,sizeof(name),1,ft);
fclose(pFile);
fclose(ft);
remove("records.bin");
rename("temp.dat","records.bin");
}

/* Delete records for a given name */
/* To delete one or more records we can copy 
   the contents of the existing file to a new 
   file, omitting the records that are to be
   deleted. We can then delete the old file and
   rename the new file to have the old file
   name.
*/
void delete_records(struct Name name)
{
  FILE *pNewFile = NULL;
  char *pnewfilename = NULL;
  struct PhoneRecord record;

  if((pFile = fopen(filename, "r")) == NULL)         /* Open current file to read it */
  {
    printf("Error opening %s for reading. Program terminated.", filename);
    abort();
  }

  pnewfilename = tmpnam(NULL);                       /* Create temporary file name      */
  if((pNewFile = fopen(pnewfilename, "w")) == NULL)  /* Open temporary file to write it */
  {
    printf("Error opening %s for writing. Program terminated.", pnewfilename);
    fclose(pFile);
    abort();
  }

  /* Copy existing file contents to temporary file, omitting deleted records */
  for(;;)
  {
    fread(&record, sizeof record, 1, pFile);      /* Read a record      */
    if(feof(pFile))                               /* End of file read ? */
      break;                                      /* Yes-quit copy loop */

    if(equals(name, record.name))                 /* Is the record this name ?      */
    {
      printf("\nFound a record:");                /* Ys, so it's a delete candidate */
      show_record(record);
      printf("\nDo you really want to delete it(y or n)? ");
      scanf(" %c", &answer);
      if(answer == 'y')                  /* If it's to be deleted */
        continue;                                 /* Skip the copying      */
    }
    
    fwrite(&record, sizeof record, 1, pNewFile);  /* copy current record   */
  }
  printf("Not found any record(s):\n"); 
  fclose(pFile);
  fclose(pNewFile);

  if((pNewFile = fopen(pnewfilename, "r")) == NULL)   /* Open temporary file to read it */
  {
    printf("Error opening %s for reading. Program terminated.", pnewfilename);
    abort();
  }
  if((pFile = fopen(filename, "w"))==NULL)            /* Open original file to write it */
  {
    printf("Error opening %s for writing. Program terminated.", filename);
    abort();
  }

  /* Copy contents of new temporary file back to old file          */
  /* This overwrites the original contents because the mode is "w" */
  for(;;)
  {
    fread(&record, sizeof record, 1, pNewFile);   /* Read temporary file */
    if(feof(pNewFile))                            /* If we read EOF */
      break;                                      /* We are done    */

    fwrite(&record, sizeof record, 1, pFile);     /* Write record to original file */
  }
  fclose(pFile);                       /* Close the original file   */
  fclose(pNewFile);                    /* Close the temporary file  */
  remove(pnewfilename);                /* Delete the temporary file */
  printf("Delete complete.");
}

//edit function




void edit_records(struct Name name)
{
  FILE *pNewFile = NULL;
  char *pnewfilename = NULL;
  struct PhoneRecord record;

  if((pFile = fopen(filename, "r")) == NULL)         /* Open current file to read it */
 {
    printf("Error opening %s for reading. Program terminated.", filename);
    abort();
  }

  pnewfilename = tmpnam(NULL);                       /* Create temporary file name      */
 if((pNewFile = fopen(pnewfilename, "w")) == NULL)  /* Open temporary file to write it */
 {
    printf("Error opening %s for writing. Program terminated.", pnewfilename);
    fclose(pFile);
    abort();
  }

  //Copy existing file contents to temporary file, omitting deleted records */
 for(;;)
  {
    fread(&record, sizeof record, 1, pFile);      /* Read a record      */
    if(feof(pFile))                               /* End of file read ? */
      break;                                      /* Yes-quit copy loop */

   if(equals(name, record.name))                 /* Is the record this name ?      */
    {
      printf("\nFound a record:");                /* Ys, so it's a delete candidate */
     show_record(record);
      printf("\nDo you really want to Edit it(y or n)? ");
      scanf(" %c", &answer);
      if(answer == 'y')                  /* If it's to be deleted */
        continue;                                 /* Skip the copying      */
    }
    fwrite(&record, sizeof record, 1, pNewFile);  /* copy current record   */
  }
  fclose(pFile);
 fclose(pNewFile);

if((pNewFile = fopen(pnewfilename, "r")) == NULL)   /* Open temporary file to read it */
 {
    printf("Error opening %s for reading. Program terminated.", pnewfilename);
    abort();
  }
  if((pFile = fopen(filename, "w"))==NULL)            /* Open original file to write it */
  {
    printf("Error opening %s for writing. Program terminated.", filename);
   abort();
 }

  /* Copy contents of new temporary file back to old file          */
  /* This overwrites the original contents because the mode is "w" */
 for(;;)
 {
    fread(&record, sizeof record, 1, pNewFile);   /* Read temporary file */
    if(feof(pNewFile))                            /* If we read EOF */
      break;                                      /* We are done    */

   fwrite(&record, sizeof record, 1, pFile);     /* Write record to original file */
  }
  fclose(pFile);                       /* Close the original file   */
 fclose(pNewFile);                    /* Close the temporary file  */
 remove(pnewfilename);
rename("temp.dat","records.bin");                /* Delete the temporary file */
  printf("Edited successfully.");
}






/* List all the records in the file */
void list_records()
{
  struct PhoneRecord record;
  int file_empty = TRUE;    /* File empty flag */

  if((pFile = fopen(filename, "r")) == NULL)   /* Open the file to read it */      
  {
    printf("Error opening %s for reading. Program terminated.", filename);
    abort();
  }

  /* List the file contents */
  for(;;)
  {
    fread(&record, sizeof record, 1, pFile);
    if(feof(pFile))
      break;
    file_empty = FALSE;          /* We got a record so set empty flag false */
    show_record(record);         /* output the record                       */
  }
  fclose(pFile);                 /* Close the file */

  /* Check whether there were any records */
  if(file_empty)
    printf("The file contains no records.\n");
  else
    printf("\n");
}

/* Displays the operations that are supported by the program */
void show_operations()
{

  printf("*******************************ADDRESS BOOK*************************************"
  "\n\n The available funtions to perform are:"
    "\n\n\tA: Add a new name and number entry."
    "\n\n\tE: Edit name and number entry."
    "\n\n\tD: Delete all existing entries for a name."
    "\n\n\tF: Find the number(s) for a given name."
    "\n\n\tL: List all the entries in the file."
    "\n\n\tQ: Quit the program.\n\n");
}

/* Find numbers corresponding to a given name */
struct PhoneRecord find_numbers(struct Name name) 
{
  struct PhoneRecord record;
  int name_found = FALSE;                    /* Name found flag */

  if((pFile = fopen(filename, "r")) == NULL) /* Open the file to read it */      
  {
    printf("Error opening %s for reading. Program terminated.", filename);
    abort();
  }

  /* Search the records read from the file */
  for(;;)
  {
    fread(&record, sizeof record, 1, pFile);  /* Read a record */
    if(feof(pFile))
      break;
   if(equals(name,record.name))               /* Is it the name requested? */
   {
     if(!name_found)                          /* Is this the first time we found it? */
     {
       name_found = TRUE;                        /* Yes so set flag to true */
       printf("The numbers for this name are:"); /* Output initial message  */
     }
     printf("\n%s",record.number);               /* Output the number       */
   }
  }
  fclose(pFile);                                 /* Close the file */

  /* Check for name not found */
  if(!name_found)
    printf("The name was not found.\n");
  else
    printf("\n");
}

/* Compare two names for equality */
int equals(struct Name name1, struct Name name2)
{
  return (strcmp(name1.firstname, name2.firstname)==0) && (strcmp(name1.secondname, name2.secondname)==0);
}

it is not editing reocrds
help me out

Learn to use your compiler's debugger so that you can single-step through the program and find out why it isn't working. What compiler are you using?

line 316: After closing both files, instead of reopening them and copying the new data back it would be a lot easier and quicker to delete the original file and rename the temp file to the name of the original file. No copying necessary.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.