1
134

파일 입출력을 라이브러리 입출력에서 시스템 입출력으로 바꾼 코드입니다.



//***************************************************************
//                   HEADER FILE USED IN PROJECT
//***************************************************************
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

//***************************************************************
//                   STURUCTURE USED IN PROJECT
//****************************************************************

struct student {
	int rollno;
	char name[50];
	int p_marks, c_marks;
	double per;
	char grade;
}st;

//***************************************************************
//        global declaration
//****************************************************************

//수정
//FILE* fptr;
int fptr;

//***************************************************************
//        function to write in file
//****************************************************************

void write_student() {
	errno_t err;

	//수정
	//err = fopen_s(&fptr, "student.dat", "ab");  //return 0 if success
	fptr = open("student.dat", O_RDWR | O_APPEND);

	printf("\nPlease Enter The New Details of student \n");
	printf("\nEnter The roll number of student ");
	scanf_s("%d", &st.rollno);
	getchar(); // flushing buffer
	printf("\n\nEnter The Name of student ");
	gets_s(st.name, sizeof(st.name));
	printf("\nEnter The marks in physics out of 100 : ");
	scanf_s("%d", &st.p_marks);
	printf("\nEnter The marks in chemistry out of 100 : ");
	scanf_s("%d", &st.c_marks);

	st.per = (st.p_marks + st.c_marks) / 2.0;
	if (st.per >= 60)
		st.grade = 'A';
	else if (st.per >= 50 && st.per < 60)
		st.grade = 'B';
	else if (st.per >= 33 && st.per < 50)
		st.grade = 'C';
	else
		st.grade = 'F';
	//수정
	//fwrite(&st, sizeof(st), 1, fptr);
	write(fptr, &st, sizeof(st));

	//수정
	//fclose(fptr);
	close(fptr);
	printf("\n\nStudent Record Has Been Created.  Press any key.... ");
	_getch();
}


//***************************************************************
//        function to read all records from file
//****************************************************************


void display_all() {
	//errno_t err; 
	int i;
	system("cls");
	printf("\n\n\n\t\tDISPLAY ALL RECORD !!!\n\n");
	printf("====================================================\n");
	printf("R.No.  Name       P   C   Ave   Grade\n");
	printf("====================================================\n");

	//수정
	//err = fopen_s(&fptr, "student.dat", "rb");
	fptr = open("student.dat", O_RDONLY);
	if (fptr == NULL)
		return;

	//수정
	//while ((i = fread(&st, sizeof(st), 1, fptr)) > 0) {
	while ((i = read(fptr, &st, sizeof(st)))) {
		printf("%-6d %-10s %-3d %-3d %-3.2f  %-1c\n",
			st.rollno, st.name, st.p_marks, st.c_marks, st.per, st.grade);
	}
	//수정
	//fclose(fptr);
	close(fptr);
	_getch();
}


//***************************************************************
//        function to read specific record from file
//****************************************************************


void display_sp(int n) {
	int flag = 0;
	//errno_t err;
	
	//수정
	//err = fopen_s(&fptr, "student.dat", "rb");
	fptr = open("student.dat", O_RDONLY);

	if (fptr == NULL)
		return;

	//수정
	//while ((fread(&st, sizeof(st), 1, fptr)) > 0) {
	while ((read(fptr, &st, sizeof(st)))) {
		if (st.rollno == n) {
			system("cls");
			printf("\nRoll number of student : %d", st.rollno);
			printf("\nName of student : %s", st.name);
			printf("\nMarks in Physics : %d", st.p_marks);
			printf("\nMarks in Chemistry : %d", st.c_marks);
			printf("\nPercentage of student is  : %.2f", st.per);
			printf("\nGrade of student is : %c", st.grade);
			flag = 1;
		}
	}

	//수정
	//fclose(fptr);
	close(fptr);
	if (flag == 0)
		printf("\n\nrecord not exist");
	_getch();
}


//***************************************************************
//        function to modify record of file
//****************************************************************


void modify_student() {
	int no, found = 0, i;
	//errno_t err;
	system("cls");
	printf("\n\n\tTo Modify ");
	printf("\n\n\tPlease Enter The roll number of student");
	scanf_s("%d", &no);

	//수정
	//err = fopen_s(&fptr, "student.dat", "rb+");
	fptr = open("student.data", O_WRONLY);

	if (fptr == NULL)
		return;

	//수정
	//while ((i = fread(&st, sizeof(st), 1, fptr)) > 0 && found == 0) {
	while ((i = read(fptr, &st, sizeof(st)) > 0 && found == 0)) {
		if (st.rollno == no) {
			printf("\nRoll number of student : %d", st.rollno);
			printf("\nName of student : %s", st.name);
			printf("\nMarks in Physics : %d", st.p_marks);
			printf("\nMarks in Chemistry : %d", st.c_marks);
			printf("\nPercentage of student is  : %.2f", st.per);
			printf("\nGrade of student is : %c", st.grade);
			printf("\nPlease Enter The New Details of student \n");
			printf("\nEnter The roll number of student ");
			scanf_s("%d", &st.rollno);
			getchar();  //flushing buffer (fflsh�� �۵�����)
			printf("\n\nEnter The Name of student ");
			gets_s(st.name, sizeof(st.name));
			printf("\nEnter The marks in physics out of 100 : ");
			scanf_s("%d", &st.p_marks);
			printf("\nEnter The marks in chemistry out of 100 : ");
			scanf_s("%d", &st.c_marks);

			st.per = (st.p_marks + st.c_marks) / 2.0;
			if (st.per >= 60)
				st.grade = 'A';
			else if (st.per >= 50 && st.per < 60)
				st.grade = 'B';
			else if (st.per >= 33 && st.per < 50)
				st.grade = 'C';
			else
				st.grade = 'F';


			//수정
			//fseek(fptr, -(int)sizeof(st), 1);  // SEEK_CUR
			lseek(fptr, -(int)sizeof(st), SEEK_CUR);

			//수정
			//fwrite(&st, sizeof(st), 1, fptr);
			write(fptr, &st, sizeof(st));


			printf("\n\n\t Record Updated");

			found = 1;
			break;
		}
	}

	//수정
	//fclose(fptr);
	close(fptr);

	if (found == 0)
		printf("\n\n Record Not Found ");
	_getch();
}


//***************************************************************
//        function to delete record of file
//****************************************************************


void delete_student() {
	int no;

	//수정
	//FILE* fptr2;
	int fptr2;

	//errno_t err;
	system("cls");
	printf("\n\n\n\tDelete Record");
	printf("\n\nPlease Enter The roll number of student You Want To Delete");
	scanf_s("%d", &no);

	//수정
	//err = fopen_s(&fptr, "student.dat", "rb");
	fptr = open("student.dat", O_RDONLY);

	if (fptr == NULL)
		return;
	//수정
	//err = fopen_s(&fptr2, "Temp.dat", "wb");
	fptr2 = open("Temp.dat", O_WRONLY);


	//수정
	//rewind(fptr);  // move file pointer to 0
	lseek(fptr, 0, SEEK_SET);

	//수정
	//while ((fread(&st, sizeof(st), 1, fptr)) > 0) {
	while ((read(fptr, &st, sizeof(st))) > 0) {
		if (st.rollno != no) {
			//수정
			//fwrite(&st, sizeof(st), 1, fptr2);
			write(fptr2, &st, sizeof(st));
		}
	}

	//수정
	//fclose(fptr2);
	close(fptr2);
	//fclose(fptr);
	close(fptr);
	remove("student.dat");
	rename("Temp.dat", "student.dat");
	printf("\n\n\tRecord Deleted ..");
	_getch();
}



//***************************************************************
//        THE MAIN FUNCTION OF PROGRAM
//****************************************************************
void main() {
	char ch;
	int num;
	do {
		system("cls");

		printf("\n\n\t1.CREATE STUDENT RECORD");
		printf("\n\n\t2.DISPLAY ALL STUDENTS RECORDS");
		printf("\n\n\t3.SEARCH STUDENT RECORD ");
		printf("\n\n\t4.MODIFY STUDENT RECORD");
		printf("\n\n\t5.DELETE STUDENT RECORD");
		printf("\n\n\t6.EXIT");
		printf("\n\n\tPlease Enter Your Choice (1-6) ");
		ch = _getche();

		switch (ch) {
		case '1':	system("cls");
			write_student();
			break;
		case '2':	display_all();
			break;
		case '3':	system("cls");
			printf("\n\n\tPlease Enter The roll number ");
			scanf_s("%d", &num);
			display_sp(num);
			break;
		case '4':	modify_student(); break;
		case '5':	delete_student(); break;
		case '6':	break;
		default:	printf("\a");
		}
	} while (ch != '6');
}
//***************************************************************
//                END OF PROJECT
//***************************************************************

fopen, fwrite, fread를 open, write, read로 수정한 코드입니다. 그런데 수정 함수와 삭제 함수에서 에러가 나는데 왜 그런지 이유를 알 수 없습니다.

0
  • 답변 1

  • 그래안그래
    1k
    2021-10-18 11:34:44

    브레이크포인트걸고 각변수에저장되어있는값을확인해봅시다

  • 로그인을 하시면 답변을 등록할 수 있습니다.