4-3 File IO - 시스템 함수 (과제-2)

do·2022년 4월 19일
0

API

목록 보기
22/42

수정
1. 구조체 50개 생성
2. 처음 오픈할때 read, 종료할때 write
3. 아이디 중복 확인
4. 저장된 정보 갯수 확인
5. 삭제
6. 과목명 체크 -> 다음 과제에서 비트로

2-3 수강신청 프로그램 파일저장 fileio1.c f.txt

#include <stdio.h>
#include <stdlib.h> //malloc() free()
#include <errno.h> //errno
#include <string.h> //strerror() strlen()
#include <fcntl.h> //open()
#include <unistd.h> //read() write() lseek() close()
#include <stdio_ext.h> //__fpurge

enum { MAX = 50, nSub = 8 };

typedef struct Info {
	int id;
	char name[13];
	char sub[150];
} Info;

const char* Subject[nSub] =
{ "컴퓨터개론", "이산수학", "C언어", "JAVA초급",
	"리눅스구조", "자료구조", "컴파일러", "네트워크개론" };

void PrintInfo(Info* info);

void ClearStdin(char* str)
{
	if (str == NULL) {
		return;
	}
	if (str[strlen(str) - 1] == '\n') {
		str[strlen(str) - 1] = '\0';
	}
	__fpurge(stdin);
}

int NameCheck(char* str)
{
	for (int i = 0; i < nSub; i++) {
		if (strcmp(Subject[i], str) == 0) {
			return 1;
		}
	}
	printf("존재하지 않은 과목명을 입력하였습니다. 메뉴로 돌아갑니다.\n");
	return 0;
}

int main()
{
	const char* path = "./f.txt";
	
	int fd = 0;
	int rd = 0;
	int wr = 0;

	int count = 0;

	int select = 0;
	int backToMenu = 0;

	int tempID = 0;
	int deleteID = 0;
	
	char search[13];
	memset(search, 0, sizeof(search));

	Info info[MAX];
	memset(info, 0, sizeof(info));

	fd = open(path, O_RDWR | O_CREAT, 0777);
	if (fd == -1) {
		fprintf(stderr, "OPEN errno[%d] : %s\n", errno, strerror(errno));
		return 0;
	}

	lseek(fd, (off_t)0, SEEK_SET);

	rd = read(fd, info, sizeof(info));
	if (rd == -1) {
		fprintf(stderr, "READ errno[%d] : %s\n", errno, strerror(errno));
		close(fd);
		return 0;
	}	
	
	for (int i = 0; i < MAX; i++) {
		if (info[i].id == 0) {
			printf("empty info[%d]\n",i);
			count = i;
			break;
		}
	}

	printf("intial count %d\n", count);
	printf("sizeof(info) %ld\n", sizeof(info));

	do {
		backToMenu = 0;
        
		printf("======================\n");
		printf("1. 수강 신청 정보 입력\n");
		printf("2. 수강 신청 정보 출력\n");
		printf("3. 종료\n");
		printf("4. 삭제\n");
		printf("======================\n");
		printf("select num: ");
		scanf("%d", &select);
		while (getchar() != '\n') {};
		if (select != 1 && select != 2 && select != 3 && select != 4) {
			printf("숫자를 다시 입력해주세요.\n");
		}
	
		switch (select) {
			case 1:
				{
					if (count >= MAX) {
						printf("최대 입력 갯수를 초과했습니다. 메뉴로 돌아갑니다.\n");
						break;
					}
					
					printf("아이디: ");
					scanf("%d", &tempID);
					while (getchar() != '\n') {};
					if (tempID < 1 || tempID > 1000) {
						printf("아이디의 범위는 1부터 1000입니다. 메뉴로 돌아갑니다.\n");
						break;
					}

					for (int i = 0; i < MAX; i++) {
						if (info[i].id == tempID) {
							printf("아이디가 중복되었습니다. 메뉴로 돌아갑니다.\n");
							backToMenu = 1;
							break;
						}
					}
					if (backToMenu == 1) {
						break;
					}

					info[count].id = tempID;			

					printf("이름: ");
					fgets(info[count].name, sizeof(info[count].name), stdin);
					ClearStdin(info[count].name);

					printf("수강 신청 과목: ");
					fgets(info[count].sub, sizeof(info[count].sub), stdin);
					ClearStdin(info[count].sub);
					int j = 0;
					for (size_t i = 0; i < sizeof(info[count].sub); i++) {
						if (info[count].sub[i] != ' ') {
							info[count].sub[j++] = info[count].sub[i];
						}
					}
					info[count].sub[j] = '\0';
					
					char copy[200];
					strcpy(copy, info[count].sub);
					info[count].sub[0] = '\0';

					char* cut = strtok(copy, ",");
					while (cut != NULL) {
						if (NameCheck(cut) == 0) {
							memset(&info[count], '\0', sizeof(info[count]));
							backToMenu = 1;
							break;
						}
						strcat(info[count].sub, cut);
						cut = strtok(NULL, ",");
						if (cut != NULL) {
							strcat(info[count].sub, ", ");
						}
					}

					if (backToMenu == 1) {
						break;
					}
					else {
						printf("info[%d] 입력\n", count);
						count++;
					}
				}
				break;
			case 2:
				{
					printf("이름 검색: ");
					fgets(search, sizeof(search), stdin);
					ClearStdin(search);

					for (int i = 0; i < count; i++) {
						if (strcmp(search, info[i].name) == 0) {
							PrintInfo(&info[i]);
						}
						else if (strcmp(search, "all") == 0) {
							PrintInfo(&info[i]);
						}
					}
				}
				break;
			case 3:
				{
					printf("종료합니다.\n");

					lseek(fd, (off_t)0, SEEK_SET);

					wr = write(fd, info, sizeof(info));
					if (wr < 1) {
						fprintf(stderr, "WRITE errno[%d] : %s\n", errno, strerror(errno));
						close(fd);
						return 0;
					}
					
					if (close(fd) == -1) {
						fprintf(stderr, "CLOSE errno[%d] : %s\n", errno, strerror(errno));
						return 0;
					}
				}
				break;
			case 4:
				{
					printf("삭제할 아이디: ");
					scanf("%d", &deleteID);
					while (getchar() != '\n') {};

					for (int i = 0; i < MAX; i++) {
						if (info[i].id == deleteID) {
							printf("info[%d] 삭제\n", i);
							if (i == count - 1) {
								memset(&info[i], 0, sizeof(info[i]));
							}
							else {
								for (int j = i; j < MAX; j++) {
									info[j].id = info[j + 1].id;
									strcpy(info[j].name, info[j + 1].name);
									strcpy(info[j].sub, info[j + 1].sub);
								}
								memset(&info[count - 1], 0, sizeof(info[count - 1]));
							}
							count--;
							break;
						}
					}
				}
				break;
		}
	} while (select != 3);

	return 0;
}

void PrintInfo(Info* info)
{
	size_t length = strlen(info->name);

	printf("ID: ");
	if (info->id < 10) {
		printf("   ");
	}
	else if (info->id < 100) {
		printf("  ");
	}
	else if (info->id < 1000) {
		printf(" ");
	}
	printf("%d, 이름: %s", info->id, info->name);
	
	if (length == 3) {
		printf("      ");
	}
	else if (length == 6) {
		printf("    ");
	}
	else if (length == 9) {
		printf("  ");
	}
	printf(", 수강신청 과목: %s\n", info->sub);
}

0개의 댓글