fscanf함수와 fprint // 학생의 데이터베이스를 만드는 프로그램
우람늘
질문 제목 : 헤더 파일 student.h
#includestdio.h
#includestring.h
#includeWindows.h
#ifndef __student_h_
#define __student_h_
typedef struct idd{
char name[20];
int id;
float score;
}ID;
FILE* infile;
#endif __student_h_
.c파일 기말프로젝트.c#includestudent.h
int index = 0;
extern int menu(ID student[]);
extern int addstudent(FILE* infile, ID student[]);
extern int deletestudent(FILE* infile,ID student[]);
extern int modifystudent(FILE* infile, ID student[]);
extern int showstudent(ID student[]);
extern int print1(ID student[]);
extern int print2(ID student[]);
extern int print3(ID student[]);
extern int firstscan(FILE *infile, ID student[]);
void main()
{
//int caseNo;
//int load;
//
ID student[100];
//infile = fopen(student.db , a+);
//fscanf(infile, %d , &caseNo);
//for(load = 0 ; loadcaseNo ; load++)
//{
//fscanf(infile , %s %d %f , student[load].name , &student[load].id, &student[load].score);
//index++;
//
//}
//fclose(infile);
firstscan(infile,student);
menu(student);
}
int menu(ID student[])
{
int order;
printf(ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ\n);
printf(1. 학생 자료 출력\n);
printf(2. 학생 자료 추가\n);
printf(3. 학생 자료 수정\n);
printf(4. 학생 자료 삭제\n);
printf(ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ\n\n);
printf(숫자를 입력하세요 ㅡㅡ );
scanf(%d , &order);
switch(order)
{
case 1:
system(cls);
showstudent(student);
break;
case 2:
system(cls);
addstudent(infile,student);
break;
case 3:
system(cls);
modifystudent(infile, student);
break;
case 4:
system(cls);
deletestudent(infile,student);
break;
}
}
.c파일 기말프로젝트2.c
#includestudent.h
extern int index;
//
int firstscan(FILE* infile, ID student[]);
int menu(ID student[]);
int deletestudent(FILE* infile,ID student[]);
int modifystudent(FILE* infile, ID student[]);
int showstudent(ID student[]);
int print1(ID student[]);
int print2(ID student[]);
int print3(ID student[]);
extern ID student[100];
int addstudent(FILE* infile,ID student[])
{
int su0;
int torf;
re:
printf(\n\n학생을 추가하려면 1을 입력하세요.\n);
printf(이전 메뉴로 가려면 0을 입력하세요.\n);
scanf(%d , &torf);
infile = fopen(student.db, w+);
if(torf == 1)
{
system(cls);
printf(\n\n이름 학번 성적순으로 입력해주세요\n);
scanf(%s %d %f , student[index].name , &student[index].id , &student[index].score);
torf = 0;
index++;
fprintf(infile, %d\n , index);
for(su0=0;su0index ; su0++)
{
fprintf(infile, %s %d %f\n, student[su0].name , student[su0].id , student[su0].score);
}
goto re;
}
else if(torf == 0)
{
system(cls);
menu(student);
}fprintf(infile, %d\n , index);
for(su0=0;su0index ; su0++)
{
fprintf(infile, %s %d %f\n, student[su0].name , student[su0].id , student[su0].score);
}
fclose(infile);
}
int showstudent(ID student[])
{
int ordshow;
printf(\n\n1. 학번순 출력\n);
printf(2. 이름순 출력\n);
printf(3. 성적순 출력\n);
scanf(%d , &ordshow);
switch(ordshow){
case 1:
print1(student);
break;
case 2:
print2(student);
break;
case 3:
print3(student);
break;
default:
printf(잘못된 입력입니다);
menu(student);
break;
}
}
int print1(ID student[])
{
int k,l,temp1,gotomenu;
char cpy1[50];
for(k = 0 ;k(index-1) ; k++)
{
for(l = 0 ; l(index-1-k) ; l++)
{
if(student[l].id student[l+1].id )
{
temp1 = student[l].score;brcore;
student[l].score = student[l+1].score;
student[l+1].score = temp1;
temp1 = student[l].id;
student[l].id = student[l+1].id;
student[l+1].id = temp1;
strcpy(cpy1,student[l].name);
strcpy(student[l].name , student[l+1].name);
strcpy(student[l+1].name , cpy1);
}
}
}
for(k=0 ; kindex ; k++)
{
printf(%s %d %f\n\n , student[k].name , student[k].id, student[k].score);
}
printf(아무 키나 입력하면 메뉴로 돌아갑니다);
scanf(%d ,&gotomenu);
switch(gotomenu){
default:
system(cls);
menu(student);
break;
}
}
int print2(ID student[])
{
int a,b,temp2,gotomenu2;
char cpy2[50];
for(a = 0 ;a(index-1) ; a++)
{
for(b = 0 ; b(index-1-a) ; b++)
{
if(strcmp(student[b].name ,student[b+1].name) 0 )
{
temp2 = student[b].score;
student[b].score = student[b+1].score;
student[b+1].score = temp2;
temp2 = student[b].id;
student[b].id = student[b+1].id;
student[b+1].id = temp2;
strcpy(cpy2,student[b].name);
strcpy(student[b].name , student[b+1].name);
strcpy(student[b+1].name , cpy2);
}
}
}
for(a=0 ; aindex ; a++)
{
printf(%s %d %f\n , student[a].name , student[a].id, student[a].score);
}
printf(아무 키나 입력하면 메뉴로 돌아갑니다);
scanf(%d ,&gotomenu2);
switch(gotomenu2){
default:
system(cls);
menu(student);
break;
}
}
int print3(ID student[])
{
int i;
int j;
int temp2,gotomenu3;
char cpy[50];
for(i = 0 ;i(index-1) ; i++)
{
for(j = 0 ; j(index-1-i) ; j++)
{
if(student[j].score student[j+1].score )
{
temp2 = student[j].score;
student[j].score = student[j+1].score;
student[j+1].score = temp2;
temp2 = student[j].id;
student[j].id = student[j+1].id;
student[j+1].id = temp2;
strcpy(cpy,student[j].name);
strcpy(student[j].name , student[j+1].name);
strcpy(student[j+1].name , cpy);
}
}
}
for(i=0 ; iindex ; i++)
{
printf(%s %d %f\n , student[i].name , student[i].id, student[i].score);
}
printf(아무 키나 입력하면 메뉴로 돌아갑니다);
scanf(%d ,&gotomenu3);
switch(gotomenu3){
default:
system(cls);
menu(student);
break;
}
}
int modifystudent(FILE* infile,ID student[])
{
int su2;
int e;
int d;
char cmpname[50];
re:
printf(변경할 학생의 이름을 입력해주세요.\n);
scanf(%s , cmpname);
for(d = 0 ; strcmp(cmpname,student[d].name)&&dindex ; d++)
1;
if(!strcmp(cmpname,student[d].name))
{
printf(%s 학생의 정보를 변경합니다. 이름 학번 점수 순으로 입력해주세요\n , student[d].name);
scanf(%s %d %f , student[d].name, &student[d].id , &student[d].score);
printf(\n변경되었습니다. 계속 변경하시려면 1, 메뉴로 돌아가시려면 0을 입력하세요.\n);
fopen(student.db , w+);
fprintf(infile, %d\n , index);
for(su2=0;su2index ; su2++)
{
fprintf(infile, %s %d %f\n, student[su2].name , student[su2].id , student[su2].score);
}
fclose(infile);
scanf(%d ,&e);
switch(e){
case 1:
goto re;
case 0:
system(cls);
menu(student);
}
}
else
{
printf(해당 이름을 가진 학생이 없습니다. 다시한번 확인해주세요.\n\n);
printf(계속 변경하시려면 1, 메뉴로 돌아가시려면 0을 입력하세요.\n);
fopen(student.db , w+);
fprintf(infile, %d\n , index);
for(su2=0;su2index ; su2++)
{
fprintf(infile, %s %d %f\n, student[su2].name , student[su2].id , student[su2].score);
}
fclose(infile);
scanf(%d ,&e);
switch(e){
case 1:
goto re;
case 0:
system(cls);
menu(student);
}
goto re;
}}
int deletestudent(FILE* infile, ID student[])
{
int su;
int g;
int f;
char cmpdname[50];
re:
printf(삭제할 학생의 이름을 입력해주세요.\n);
scanf(%s , cmpdname);for(f = 0 ; strcmp(cmpdname,student[f].name)&&findex ; f++)
1;
if(strcmp(cmpdname,student[f].name) == 0)
{
printf(\n학생의 정보를 삭제합니다\n );
for(;findex;f++)
{
strcpy(student[f].name , student[f+1].name);
student[f].id = student[f+1].id;
student[f].score = student[f+1].score;
}
index--;
printf(계속 삭제하시려면 1, 메뉴로 돌아가시려면 0을 입력하세요.\n);
fprintf(infile,%d , index);
for(su=0;suindex ; su++)
{
fprintf(infile, %s %d %f\n, student[su].name , student[su].id , student[su].score);
}
scanf(%f ,&g);
switch(g){
case 1:
goto re;
case 0:
system(cls);
menu(student);
}
goto re;
}
else
{
printf(해당 이름을 가진 학생이 없습니다. 다시한번 확인해주세요.\n\n);
printf(계속 삭제하시려면 1, 메뉴로 돌아가시려면 0을 입력하세요.\n);
fprintf(infile,%d , index);
for(su=0;suindex ; su++)
{
fprintf(infile, %s %d %f\n, student[su].name , student[su].id , student[su].score);
}
fclose(infile);
scanf(%f ,&g);
switch(g){
case 1:
goto re;
case 0:
system(cls);
menu(student);
}
goto re;
}
}
int firstscan(FILE *infile, ID student[student[])
{
int caseNo;
int load;
infile = fopen(student.db , a+);
fscanf(infile, %d , &caseNo);
for(load = 0 ; loadcaseNo; load++)
{
fscanf(infile, %s %d %f , student[load].name, &student[load].id , &student[load].score);
index++;
}
fclose(infile);
}
질문 내용 : 본 내용과 같이 학생의 정보 추가 제거 수정등이 가능한 데이터베이스 프로그램을 만들었습니다만..
후에 필요에 의해
기말프로젝트.c에 있는 주석처리된 코드를 기말프로젝트2.c에 firstscan이란 이름으로 함수로 만들었으나
이 함수를 적용시키고 다른 기능(수정,삭제)등을 시도하면
이런 오류가 뜹니다..
단순히 위치만 옮겼을 뿐인데 뭐가 문젠지 모르겠네요
처음부터 제시문을 조금 더 잘 읽고 짰더라면 이런일은 없었을텐데 아쉽네요
-
내꺼 2023-10-01
fprintf 인자에 NULL을 넣은거 아닌지 확인해 보시길...
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2685477 | 소수점이하 출력하는거요 | 널위해 | 2025-02-18 |
2685449 | printf배우고있는데요 궁금한게있어요 (8) | 꽃큰 | 2025-02-18 |
2685393 | c언어 질문이요.... | 붕붕 | 2025-02-18 |
2685365 | 윈도우7에서 visual studio c++6.0 (1) | 빛길 | 2025-02-17 |
2685317 | segmentation fault가 나요 | 가람 | 2025-02-17 |
2685292 | 다중스택질문 | 올해1살 | 2025-02-17 |
2685231 | C언어와 닷넷에 대해서 질문!! (2) | 설아 | 2025-02-16 |
2685206 | VisualBasic 과 DEV++ 의 장단점과 만든 파일 호환 가능하나요? | 에드워드 | 2025-02-16 |
2685154 | 배열 크기조절 | 해찬솔 | 2025-02-15 |
2685124 | 수정이거 제가 뭐가 잘못한거죠 ? | 아이돌 | 2025-02-15 |
2685096 | 포인터의 고수분들 모두 보세요!! 제발 ㅠ_ㅠ 헷갈려헷갈려..갈려헷.. (7) | 치킨마루 | 2025-02-15 |
2685045 | 전처리기 질문 | 치에미 | 2025-02-14 |
2685016 | 오류 좀 확인해 주시면 감사하겠습니다 | 초코맛사탕 | 2025-02-14 |
2684917 | 알고리즘을 이용한 행렬의 전치// 문제다운 문제네요. | 뿡뿡 | 2025-02-13 |
2684868 | 양방향 연결리스트에서 실행도중 죽는 이유좀 찾아주세요. (2) | 예다움 | 2025-02-13 |
2684844 | 시계프로그램인데 도저히 모르겠어요ㅠ (1) | 비내리던날 | 2025-02-12 |
2684812 | 레포트좀도와주세요ㅠㅠ (2) | 갈매빛 | 2025-02-12 |
2684780 | 채팅창 흉내내보려고하는데요 ㅜ.ㅜ (1) | 바름 | 2025-02-12 |
2684729 | 내일 시험인데 메모리 그리는것좀 도와주세요 ㅠ (1) | 상처주지마 | 2025-02-11 |
2684701 | 버퍼 관련 질문 3가지 (이전거랑 달라요) | 한국녀 | 2025-02-11 |