你好这是我的问题
FILE *sourcefile;
if ((sourcefile = fopen(argv[1],"r")) == NULL) //Opens File
{
printf("Error: Could not open %s\n",argv[1]);
return 0;
}
fseek(sourcefile, 0 , SEEK_END); // Sets file pointer at the end of the file
unsigned int fSize = ftell(sourcefile); // Determines file size by the position of file pointer
fseek(sourcefile, 0 , SEEK_SET); // Sets file pointer at the start of the file
char *buffer = (char *) malloc(fSize);
if (buffer == NULL)
{
printf("Error: Not enough system memory\n");
return 0;
}
printf("%d\n",sizeof(buffer));
printf("%d\n",fSile);
fread (buffer,1,fSize,sourcefile);
fclose (sourcefile);
我的代码只是打开一个文件并将其内容加载到内存中。问题是,当我使用
char *buffer = (char *) malloc(fSize)
它只分配 4 个字节,而不是文件的全部大小(即打开一个带有小句子的简单 txt 时为 25 个字节)。当我打印buffer
和fSize
最后的尺寸时,我分别得到 4 和 25,所以fSize
是正确的。知道为什么会这样吗?
谢谢,