Archived

This topic is now archived and is closed to further replies.

Hippokrates

Just getting crap out of my file

Recommended Posts

Hippokrates    122
I am currently writing an archive class to store my files. To add a file to an archive I am doing this:
bool CArchive::AddFile(LPSTR strFileName) {
	LPBYTE pbyFileContent = NULL;
	DWORD dwSize = 0;
	CDataPackage * pData = NULL;
	std::ifstream FileIn(strFileName, std::ios::binary);
	char c;

	while(FileIn.good()) {
		FileIn.read(&c, sizeof(char));
		dwSize++;
	}
	FileIn.close();

	pbyFileContent = new BYTE[dwSize];

	FileIn.open(strFileName, std::ios::binary);
	FileIn.read((char *)pbyFileContent, dwSize);
	FileIn.close();

	pData = new CDataPackage(m_pDebug);
	pData->Create(dwSize);
	memcpy(pData->GetData(), pbyFileContent, dwSize);

	m_Files.push_back(new ARCHIVEFILE);
	m_Files.back()->strFileName = strdup(strFileName);
	m_Files.back()->pData = pData;

	m_Header.wNumberFiles++;
	m_FileEntries.resize(m_FileEntries.size() + 1);
	strcpy(m_FileEntries.back().strFileName, strFileName);
	m_FileEntries.back().dwBegin = 0;
	m_FileEntries.back().dwEnd = 0;
	m_FileEntries.back().dwUncompressedSize = dwSize;
	
	SafeDeleteV(pbyFileContent);
	return true;
}
 
For I do not know how to determine the file size with std::ifstream I have to do it manually. But after reading the pbyFileContent buffer it still contains only rubbish. I am testing this by reading in a text file so I should be able to understand what is read in but I am not because the buffer is full of crap... Any ideas?

Share this post


Link to post
Share on other sites
daerid    354
Also, if you''re working in Win32, you can just call ::GetFileSize(LPCTSTR szFileName);, which returns a DWORD (unsigned long)

Share this post


Link to post
Share on other sites