파일 크기를 읽은 다음 그에 따라 버퍼 크기를 할당하는 코드를 작성하려고합니다.malloc()이 버퍼 크기를 할당하지 않습니다.
어떤 이유로 버퍼 크기가 4 바이트를 항상 변경하지 않습니다.
버퍼가 읽기 파일 크기와 동일한 메모리를 할당하지 못하는 이유는 무엇입니까?
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *file,*fp;
file = fopen("file.txt", "r");
fp = fopen("outputnew.txt" , "w");
long lSize;
fseek(file, 0, SEEK_END);
lSize = ftell(file);
fseek(file, 0, SEEK_SET);
char *buffer = (char*) malloc(sizeof(char)*lSize);
printf("%d\n",sizeof(buffer));
return(0);
}
힌트 # 1 : [malloc'을 캐스트하지 마십시오] (http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc); 힌트 # 2 : ['sizeof (char)'는 항상'1']] (http://stackoverflow.com/questions/2215445/are-there-machines-where-sizeofchar-1) – Crozin