c語言使用文件操作創建數據庫 (Create database using file operation in c language)


問題描述

c語言使用文件操作創建數據庫 (Create database using file operation in c language)

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:

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.

(by Krunal Gandhibetabandido)

參考文件

  1. Create database using file operation in c language (CC BY‑SA 3.0/4.0)

#file-handling #Database #C






相關問題

c語言使用文件操作創建數據庫 (Create database using file operation in c language)

使用jsp瀏覽文件和文件夾 (browsing files and folders using jsp)

在 rails/paperclip 中處理一系列圖像 (handling an array of images in rails/paperclip)

Java 並行文件處理 (Java Parallel File Processing)

Perl 隱式關閉重置 $. 多變的 (Perl implicit close resets the $. variable)

更換和存放 (Replacing and Storing)

逐行讀取文件並基於它將換行符寫入同一文件 - nodejs (Reading a file line by line and based on it write newlines to same file - nodejs)

使用 PHP 將數據放到服務器上(新的 DOMdocument 不起作用) (Use PHP to put data onto server ( new DOMdocument not working))

表示“目錄”或“文件”的詞是什麼? (What is the word that means "directory" or "file"?)

如何在我的計算機上保存我用 Python 編輯的 CSV 文件? (How do I save a CSV file on my computer which i have edited in Python?)

使用文件中的類和對象獲取信息 (Get info using class and object from file)

使用 putw() 時在文件中獲取亂碼 (Getting gibberish values in files when putw() is used)







留言討論