讀文件時提前獲取文件大小以方便申請空間

char* LoadFile(const char* filename){
	char* content = NULL;
	unsigned int size = 0;

	if (access(filename, F_OK) == 0){
		FILE *fp = fopen(filename, "rb");
		if(fp != NULL){
			fseek(fp, 0L, SEEK_END);
			size = ftell(fp);
			fseek(fp, 0L, SEEK_SET);
			content = (char*)malloc(size+1);
			if(content!= NULL){
				fread(content, 1, size, fp);
				content[size]=0;
			}
			fclose(fp);
		}
	}
	return content;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章