fwrite,fread질문입니다.
Creative
#includestdio.h
#includestdlib.h
#includestring.h
#define classes 3
//#define stu_num 100
struct student{
int id;
char name[20];
int score[classes];
int total;
};
//처음에 입력할 구조체
typedef struct student std;
/*
struct record{
int id;
char name[20];
int score[classes];
int total;
};
//나중에 fread로 읽어와서 저장할 곳
typedef struct record reco;
*/
int count = 0 ; //학생 id 번호를 위한 전역변수.
void menu(void);
void insert_data();
void search_data();
main()
{
menu();//메뉴 함수호출
}
void menu(void)//메뉴 함수.
{
int num;
while(1)
{
printf(1. student data insertion\n);
printf(2. delete student data\n);
printf(3. search student data\n);
printf(4. show all the student data\n);
printf(5. exit\n);
printf(please select a number :);
scanf(%d,&num);
switch(num)
{
case 1:
insert_data();
break;
case 2:
break;
case 3:
search_data();
break;
case 4:
break;
case 5 :
printf(exit \n);
exit(1);
}
}
}
void insert_data()//학생 데이터 입력 함수.
{
std table; //typedef로 선언된 구조체에 대한 변수.나중에 지역변수로 정의해바야함.
file* file;
int state; // 파일을 닫을때 에러 체크를 위한 변수.
count++;
table.id = count;
printf(1.input student name to add : );
scanf(%s,table.name);
//이름 적는 부분
printf(score of english ? );
scanf(%d,&table.score[0]);
printf(score of maths ? );
scanf(%d,&table.score[1]);
printf(score of korean ? );
scanf(%d,&table.score[2]);
//과목 점수 입력 하는 부분table.total = table.score[0] + table.score[1] + table.score[2];
//과목의 총점을 계산 하는 부분.
file = fopen(table.txt,a);
if(file == null)
{
printf(file open error !\n);
exit(1);
}
//파일이 안 열렸을 경우의 예외처리
fwrite(&table,sizeof(table),1,file);
//입력한 학생 정보를 파일에다가 출력.
printf(write end\n);
state = fclose(file);
if(state !=0)
{
printf(file close error!\n);
exit(1);
}
//파일을 닫을때 예외 처리 부분.
}
void search_data()
{
std table;
file* file;
char* pcheck;
char name[20];
//reco reco_table;printf(3. input student name to search :);
scanf(%s,name);
file = fopen(table.txt,r);
//읽기 전용 모드.
if(file ==null)
{
printf(file open error!\n);
exit(1);
}
while(1)
{
fread(&table,sizeof(table),1,file);
pcheck = strstr(table.name,name);
if(pcheck == null)
{
printf(cannot find this student!\n);
break;
}
else
{
printf(%d %s %d %d %d %d\n,table.id,table.name,table.score[0],table.score[1],table.score[2],table.total);
break;
}
}
}
구현하고 싶은 기능은 학생 성적표 테이블이고요. 구조체로 선언을 하였고 1번 메뉴 눌러서 학생 정보를 입력해서 fwrite로 파일에다가 쓰고
메뉴에서 3번을 누르면 검색을 하는겁니다. 3번 누르고 사람 입력을 적고요 . 무한 루프 들어가서 다시 table구조체에다가 파일에 저장되어 있는 학생들 정보를 하나씩 불러옵니다. 그리고 strstr함수를 사용해서 불러온 학생중에 제가 찾고자 하는 학생이 있는지 체크를 하고 있을 경우 id와 이름 3과목에 대한 성적 합계를 출력하는거예요..
fprintf,fscanf대신 fwrite,fread를 사용하라고 해서 나름 미친듯이 찾아보고 했는데 아직 fwrite,fread에 대해 잘 모르겠네요 ㅠㅠ
그리고 저렇게 해서 저장을 하고 나서 파일을 보니깐 영어는 나오는데 숫자 부분은 다 깨지더라고요..원래 그런건가요?ㅠ.
그리고 저렇게 구현후 제가 학생이름을 검색하면 아예 찾지를 못하네요..
허접한 실력이지만 사이트에서 많이 배우고 있으니 잘못된 부분 자세한 설명과 수정좀 부탁드리겠습니다 ~
황사가 많이 오고 있으니 사이트분들 모두 건강관리 잘하시고요 ~ 부탁드릴게요 ㅠ
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2695738 | fopen과fclose질문~~ (5) | 희선 | 2025-05-23 |
2695707 | 3의 배수 나타내기. (2) | 수리 | 2025-05-23 |
2695626 | 피보나치수열 과제 때문에 질문 드립니다. (6) | 옆집언니 | 2025-05-22 |
2695595 | 포인트공부중입니다 int형에서 4=1 인가요? (3) | 족장 | 2025-05-22 |
2695567 | 드라이브 고유번호를 가져오는 함수 (2) | 초코맛사탕 | 2025-05-21 |
2695533 | 음수의 산술변환! 질문이요 ㅠㅠ... (4) | 꽃여름 | 2025-05-21 |
2695506 | 구조체 배열 이용 도서목록 출력 프로그램 (1) | 가을귀 | 2025-05-21 |
2695450 | c언어 함수 질문이요.... | 이슬비 | 2025-05-20 |
2695403 | VirtualAlloc함수 및 메모리 질문 | 크리에이터 | 2025-05-20 |
2695355 | c언어 for함수 | 미쿡 | 2025-05-19 |
2695327 | 안녕하세요 제가 이번에 좀 큰 프로그램을.. | 악당 | 2025-05-19 |
2695295 | mutex동기화의 thread기반 채팅 서버소스 질문입니다 | 그루터기 | 2025-05-19 |
2695270 | 질문이요..swap 관한겁니다..ㅠㅠ (3) | 콩알녀 | 2025-05-19 |
2695244 | 노땅초보궁금한게 하나 있는데요..반복문(while문)초보자질문 (6) | 큰꽃늘 | 2025-05-18 |
2695166 | do while 문 어떤것이잘못된건지 모르겠어요 (2) | 아이폰 | 2025-05-18 |
2695122 | 구조체에 대해 물어보고 싶은게 있습니다 ^^^.. (7) | 수련 | 2025-05-17 |
2695091 | txt 파일 입출력 후 2차 배열에 저장하기입니다. (3) | 헛장사 | 2025-05-17 |
2695063 | 수도요금 프로그램좀 짜주세요. | 시내 | 2025-05-17 |
2695033 | 답변좀요ㅠㅠ (1) | 비사벌 | 2025-05-16 |
2695010 | C++의 STL은 왜 굳이 템플릿화 시켜서 라이브러리를 만드나요? (초보수준의 질문..) (2) | 엘보어 | 2025-05-16 |