fopen画像ファイルのデータとサイズの取得

704 ワード

参考fopen,fread fwrite関数読み書きバイナリファイル問題まとめ
C取得ファイルのサイズ
#include "stdafx.h"
#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
	FILE* fp = NULL;
	int nFileLen = 0;

	fp = fopen("Test.jpg", "rb");

	if (fp == NULL)
	{
		cout << "can't open file" << endl;
		return 0;
	}

	fseek(fp,0,SEEK_END); //       
	if ((nFileLen = ftell(fp))<1)//    
	{
		fclose(fp);
		return 0;
	}
	
	char* data = (char *)malloc(sizeof(char)*(nFileLen+1));
	if (NULL == data)
	{
		fclose(fp);
		return 0;
	}
	fread(data, nFileLen, 1, fp);

	free(data);
	fclose(fp);
	return 0;
}

)
return 0;
}