<C언어>
오늘 수업 정리에 앞서서 이전 수업표를 한번더 참고 하면 좋을꺼 같아서 그림 첨부 했습니다.
![](https://t1.daumcdn.net/cfile/tistory/2758B24C5188A4B815)
![](https://t1.daumcdn.net/cfile/tistory/244B0F4C5188A4B92C)
![](https://t1.daumcdn.net/cfile/tistory/2156494C5188A4B91A)
![](https://t1.daumcdn.net/cfile/tistory/0346CE4C5188A4BA30)
====================================================================================================================
![](https://t1.daumcdn.net/cfile/tistory/261C17435188B49E2C)
구조체 입출력은 디스크의 공간을 미리 알 수 있는 장점이 있어서 DB시스템의 하부 저장구조를 만들 때 적합하다.
다른 장점은 기본자료형 뿐만 아니라 배열이나 구조체와 같은 복합 자료형의 데이터를 하나의 명령문으로 저장할 수 있다는 것이다
![](https://t1.daumcdn.net/cfile/tistory/024AB24C5188A4BA2C)
![](https://t1.daumcdn.net/cfile/tistory/263E20465188B4660B)
p11-7 .c
#include <stdio.h> #include <string.h> //strlen() 함수의 사용 #include <process.h> //exit() 함수의 사용 typedef struct { char name[25]; int english; int math; float average; } STUDENT;
int main() { STUDENT st; FILE *fp; //쓰기전용 파일의 오픈 if((fp = fopen("d11-7.dat", "wb")) == NULL) { printf("Can't open file d11-7.dat"); exit(-1); } //키보드로부터 학생정보를 입력받아 파일에 기록 printf("Please type <enter key> for Name, if you finish input\n"); printf("Enter Name: "); gets(st.name); while(strlen(st.name) > 0) //엔터 키를 누르면 입력이 종료됨 { printf("Enter English score: "); scanf("%d", &st.english); printf("Enter Math score: "); scanf("%d", &st.math); st.average = ((float)st.english + st.math) / 2; fwrite(&st, sizeof(st), 1, fp); //레코드를 파일에 기록 if(ferror(fp) == 1) //fwrite() 함수의 실행시 오류 처리 부분 { perror("Error: "); //윈도우 에서 자동적으로 에러 메세지를 출력 fclose(fp); exit(-1); } printf("\nPlease type <enter key> for Name, if you finish input\n"); printf("Enter Name: "); fflush(stdin); //버퍼내용을 화면에 출력, 비워주기 용도 gets(st.name); } fclose(fp);
//파일을 다시 열어 내용을 화면에 출력
if((fp = fopen("d11-7.dat", "rb")) == NULL) { printf("Can't open file d11-7.dat"); exit(-1); }
printf("\nName English Math Average\n"); while(fread(&st, sizeof(st), 1, fp) == 1) //파일에서 레코드 읽기 { printf("%s\t %d\t %d\t %7.2f\n", st.name, st.english, st.math, st.average); } fclose(fp);
return 0; }
|
![](https://t1.daumcdn.net/cfile/tistory/2158474C5188A4BB18)
p11-8.c
#include <stdio.h> #include <conio.h> #include <stdlib.h>
int main(int argc, char *argv[]) { char ch = 0; long pos; FILE *infp; FILE *outfp; if(argc != 3) //명령어라인의 매개변수는 3개이다. { printf("Data file name missing!"); exit(-1); } if((infp = fopen(argv[1], "rb")) == NULL) //이진모드로 입력파일 오픈 { printf("cannot fileopen"); exit(-1); } if((outfp = fopen(argv[2], "wb")) == NULL) //이진모드로 출력파일 오픈 { printf("cannot create"); exit(-1); }
while(fgetc(infp) != '\n'); //첫 행의 끝까지 읽기 pos = ftell(infp); //파일의 현재위치 파악
while(ch != '\n') { fseek(infp, pos++, SEEK_SET); //파일 현재위치 설정 ch = fgetc(infp); //문자읽기 putch(ch); //화면에 출력 fputc(ch, outfp); //출력파일에 기록
} fclose(infp); fclose(outfp); return 0; }
|
![](https://t1.daumcdn.net/cfile/tistory/23598C4C5188A4BB13)
====================================================================================================================
<ARM>
![](https://t1.daumcdn.net/cfile/tistory/222DB5425188B5381B)
![](https://t1.daumcdn.net/cfile/tistory/243480425188B5390D)
![](https://t1.daumcdn.net/cfile/tistory/21297D425188B53922)
![](https://t1.daumcdn.net/cfile/tistory/012619425188B53929)
ps : 다소 이상하고 부족하지만 금일 정리는 이만 마치겠습니다. 잘못된 부분 지적이나 부족한점 알려주시면 감사하겠슴돠~!