함수 어떻게 만들어야할지....
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define SIZE 5
typedef struct student{
int number;
char name[10];
double grade;
}student;
char *fileName = "students.txt";
student *readStudentsFromFile(FILE *dataFile); //파일에서 학생 정보를 읽어서 구조체 배열에 저장
void printStudentList(student *list); //구조체 배열에 저장된 모든 학생들의 정보를 출력
void printStudent(student *list, int index); //구조체 배열에 저장된 한명의 학생 정보를 출력
int searchStudent(char * name, student *list); //이름으로 학생을 검색 후 인덱스를 반환
int changeStudent(char * name, student *list); //이름으로 학생 검색 후 해당 학생정보를 수정
int writeStudentsToFile(student *list, FILE * targetFile); //구조체 배열에 저장된 학생 정보를 파일에 저장
void main()
{
char c;
char name[10];
int index = 0;
FILE *dataFile = fopen(fileName, "r");
FILE *writeDataFile;
student *studentList = (student *)malloc(sizeof(student) * SIZE);
studentList = readStudentsFromFile(dataFile);
fclose(dataFile);
printf("********** 명령어 ********** \n");
printf("P: 모든 학생 출력, S: 학생 검색\n");
printf("C: 학생 정보 수정\n");
printf("W: 파일에 쓰기, Q: 프로그램 종료\n");
while(1) {
printf("\nCommand>");
c = getch();
putch(c);
c = toupper(c);
switch (c) {
case 'P' :
printStudentList(studentList);
break;
case'S' :
printf("\n검색할 학생의 이름을 입력하세요 >");
scanf("%s", name);
index = searchStudent(name, studentList);
if(index >=0) {
printStudent(studentList, index);
}
else {
printf("검색에 실패 했습니다.\n");
}
break;
case 'C' :
printf("\n정보를 수정 할 학생의 이름을 입력하세요 >");
scanf("%s", name);
if(changeStudent(name, studentList) == 1) {
printf("변경이 완료되었습니다. \n");
} else {
printf("변경에 실패했습니다.\n");
}
break;
case 'W' :
writeDataFile = fopen(fileName, "w");
if(writeStudentsToFile(studentList, writeDataFile) == 1) {
printf("\n구조체배열의 학생정보를 %s 파일에 저장하였습니다. \n", fileName);
} else {
printf("저장에 실패하였습니다. \n");
}
fclose(writeDataFile);
break;
case 'Q' :
printf("\n");
exit(1);
break;
default :
printf("잘못된 명령어 입니다! \n");
break;
}
}
}
student* readStudentsFromFile(FILE *dataFile) //파일에서 학생 정보를 읽어서 구조체 배열에 저장
{
FILE *dataFile;
student s[SIZE];
dataFile = fopen("student.txt", "r");
for(int i=0; i<SIZE; i++)
{
fscanf(dataFile, "%d %s %lf", &s[i].number, s[i].name, s[i].grade);
}
fclose(dataFile);
}
닉넴많이봣는데 흠
게타였나
, 졸기네 [from DCHub WP8.0.10211.0]