Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8015141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:10:02+00:00 2026-06-04T20:10:02+00:00

I have created a database using files. When inserting a record in the file

  • 0

I have created a database using files. When inserting a record in the file if the record already exists in the file, it will end up being duplicated. But I want to avoid that. How can I design a mechanism to avoid duplicating records (i.e., how to check if the record is already in the file and how to stop the same data being entered again by user in the file)?

/*  vehicle record program      */

#include <stdio.h>
#include <string.h>

typedef struct vehicle
{
    char name[100];
    int lice_no;
    int vehicle_type;
    char cmpny_name[100];
    int menu_year;
}record;

int main(void)
{
    int i , choice;
    FILE *fp1,*fp2;
    char oname[100];
    record det;
    int recsize;
    char c;

    fp1 = fopen("record.dat" , "r+");
    if(fp1 == NULL)
    {
        fp1 = fopen("record.dat" , "w+");
        if(fp1 == NULL)
        {
            printf("error in opening file : \n");
            return -1;
        }
    }
    recsize = sizeof(det);

    fseek(fp1 , 0 ,SEEK_END);
    printf("Enter owner Name    : ");
    scanf("%[^\n]" , det.name);
    printf("Enter licence number    : ");
    scanf("%d" , &det.lice_no);
    printf("Enter the vehicle type  : ");
    scanf("%d" , &det.vehicle_type);
    scanf("%c" , &c);
    printf("Enter company name  : ");
    scanf("%[^\n]" , det.cmpny_name);
    printf("Enter menufecture year  : ");
    scanf("%d" , &det.menu_year);
    fwrite(&det,recsize,1,fp1);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:10:04+00:00Added an answer on June 4, 2026 at 8:10 pm

    Before inserting a new record, you must check whether the same record is already in the file. In order to do that, I would start by creating several functions. Something like:

    void find_record(char *name, record *rec, FILE *f);
    void add_record(const record *rec, FILE *f);
    void del_record(char *name, FILE *f);
    

    I am assuming that name alone identifies a given record. Otherwise, you will need to use a composite key (e.g., name + lice_no).

    Once you have all those functions, preventing a duplicate record becomes much easier:

    void add_record(const record *rec, FILE *f) {
        ...
        record *r;
        find_record(rec->name, r, f);
        if (r == NULL) {
            // record is not already in the file -> insert it
            ...
        }
        else {
            // record is already in the file -> do nothing
            printf("A record with name %s already exists\n", r->name);
        }
        ...
    }
    

    As you can see, programming in a modular way really helps a lot, and makes things much easier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have created table using Sqlite3.and also inserted record in that table.its running successfully.but
I have created a MySQL database using MySQL Workbench. It has about 20 tables.
I have a database that I have created using SQL Server Developer 2008. I
I have created a new application using Entity Framework 4.3 database migrations. The migrations
I am new to using the SQLite database in iphone apps. I have created
I have a DB that I created using the OOB database initializer, and I
I created a database using GoDaddy's online database manager, but I can't query it
Im using c# .net windows form application. I have many databases created using sql
Im using c# .net windows application form. I have created many databases with many
I have created my database, and used cake bake my_project to create my project,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.