출처 : 반크_세계유산 고인돌
참고풀이]
//Dev-C++ 5.11로 작업함.
#include <stdio.h>
#include <stdlib.h> //exit()
int main(void)
{
FILE *fp1, *fp2;
int JungSu[100];
short int JungSu_Short[100];
int i;
// int형 이진 파일을 읽기 모드로 연다.
if ((fp1 = fopen("jungsu.bin", "rb")) == NULL)
{
fprintf(stderr, "jungsu.bin을 열 수 없습니다.\n");
exit(1);
}
// short형 이진 파일로 저장할 파일을 쓰기 모드로 연다.
if ((fp2 = fopen("short_int.bin", "wb")) == NULL)
{
fprintf(stderr, "short_int.bin을 열 수 없습니다.\n");
exit(1);
}
//int형 자료들을 읽어온다.
fread(JungSu,sizeof(int),100,fp1);
//int형을 short형으로 변경한다.
for(i=0;i<100;i++)
JungSu_Short[i]=(short)JungSu[i];
//이진 파일로 저장한다.
fwrite(JungSu_Short,sizeof(short), 100, fp2);
fclose(fp1);
fclose(fp2);
//두 파일의 용량을 비교한다.
system("dir jungsu.bin, short_int.bin");
return 0;
}
참고풀이 결과]
읽어올 파일)
대한민국의 아름다운 영토, 독도의 여름
댓글