파일의 크기만큼 공간 할당하는 방법
int main()
{
char *buffer;
int size;
int count;
FILE *fp = fopen("hello.txt", "r"); // hello.txt 파일을 읽기 모드(r)로 열기.
// 파일 포인터를 반환
fseek(fp, 0, SEEK_END); // 파일 포인터를 파일의 끝으로 이동시킴
size = ftell(fp); // 파일 포인터의 현재 위치를 얻음
buffer = malloc(size + 1); // 파일 크기 + 1바이트(문자열 마지막의 NULL)만큼 동적 메모리 할당
memset(buffer, 0, size + 1); // 파일 크기 + 1바이트만큼 메모리를 0으로 초기화
fseek(fp, 0, SEEK_SET); // 파일 포인터를 파일의 처음으로 이동시킴
count = fread(buffer, size, 1, fp); // hello.txt에서 파일 크기만큼 값을 읽음
printf("%s size: %d, count: %d\n", buffer, size, count);
// Hello world! size: 13, count: 1: 파일의 내용, 파일 크기, 읽은 횟수 출력
fclose(fp); // 파일 포인터 닫기
free(buffer); // 동적 메모리 해제
return 0;
}