1
我需要讀取文本文件並將數據存儲到Safearray中。將文本文件讀入Safearray
我這個函數試了一下:
bool Parse::LoadTxtFileIntoSafearray(string* strPath, SAFEARRAY** pByteArray)
{
bool bReturn = false;
string line;
int iOffset = 0;
char* pcBuffer = NULL;
std::ifstream infile (strPath->data());
if (!infile.fail())
{
infile.seekg(0, std::ios::end);
int iSize = infile.tellg();
infile.seekg(0);
pcBuffer = (char*)calloc(iSize,sizeof(char));
if(pcBuffer)
{
infile.read(pcBuffer, iSize);
iSize = strlen(pcBuffer);
SAFEARRAYBOUND sb;
sb.cElements = iSize;
sb.lLbound = 0;
*pByteArray = SafeArrayCreate(VT_UI1,1,&sb);
BYTE* pData;
SafeArrayAccessData(*pByteArray,(void **)&pData);
memcpy(pData, pcBuffer,iSize);
SafeArrayUnaccessData(*pByteArray);
delete pcBuffer;
pcBuffer = NULL;
bReturn = true;
}
}
return bReturn;
}
的問題是,該文件的大小比讀取的字節大...
有什麼不對?
問候 camelord
偏離主題,但爲什麼你用'calloc'使用'delete'? – Naveen 2010-03-18 10:44:48
什麼是「safearray」? – 2010-03-18 10:47:56
沒有理由..也可以創建一個本地緩衝區..我改變它。 – camelord 2010-03-18 10:48:26