C로 접근하는 DB


Insert


//main.c

#include <stdio.h>
#include "insert.h"

void menu();               // Call Menu

void main()
{
    int choice;

    menu();
    while (1)
    {
        printf("\n메뉴 입력: ");
        scanf_s("%d", &choice);
        if (choice == 6)
            break;
        getchar();

        if (choice == 1)
        { // Insert
            insert();
        }
        else if (choice == 2)
        { // Select
        }
        else if (choice == 3)
        { // Where
        }
        else if (choice == 4)
        { // Delete
        }
        else
        { // Update
        }
    }
}

void menu()
{
    printf("================================================================================\n");
    printf("1. insert \t 2. select \t 3. where \t 4. delete \t 5. update \t 6. out\n");
    printf("================================================================================\n");
}


// insert.h
#pragma once
void insert();


// insert.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Data Segment
typedef struct project
{
    char name[20];
    char dept[20];
    int no;
} PROJECT;

static int AUTO_INCREASEMENT = 0; // 행 증가 수

PROJECT insert_info()
{
    PROJECT p;

    printf("이름 : ");
    gets(p.name);
    printf("부서 : ");
    gets(p.dept);
    p.no = ++AUTO_INCREASEMENT;

    return p;
}

void insert_record(PROJECT p)
{
    char path[30] = "file_io/";
    char number[10];
    char table_name[] = "pjt.txt";

    sprintf_s(number, sizeof(number), "%d", AUTO_INCREASEMENT);


    strcat_s(path, sizeof(path), number);
    strcat_s(path, sizeof(path), table_name);

    FILE* fp;
    fopen_s(&fp, path, "w");

    // 이름
    char name[20] = "";
    strcpy_s(name, sizeof(name), p.name);
    fputs(name, fp);
    fputs("\n", fp);

    // 부서
    char dept[20] = "";
    strcpy_s(dept, sizeof(dept), p.dept);
    fputs(dept, fp);
    fputs("\n", fp);

    // 인덱스
    fputs(number, fp);

    fclose(fp);
}

void insert()
{
    PROJECT p = insert_info();
    insert_record(p);
}