Hey,

I was wondering if someone could help me out here! I am trying to sort a list of files and remove the last x ones if it exceeds a constant variable (maxbuffersize). Below you can see what I did: I tried to sort the vector based on the predicate sortOnDate(). It compiles but when I try to run the application it crashes: Debug Assertion Failed! Expression: invalid operator< ....

Can anyone explain why it crashes? Thanks in advance! dennis

const int MAXBUFFERSIZE=100;

bool sortOnDate(const std::string& a, const std::string& b){
	
	HANDLE hFileA, hFileB; 
	WIN32_FIND_DATAA FileInformation;
	
	hFileA = ::FindFirstFileA(a.c_str(), &FileInformation);
	hFileB = ::FindFirstFileA(b.c_str(), &FileInformation);

	FILETIME ftA, ftB; 
	GetFileTime(hFileA, &ftA, NULL, NULL); 
	GetFileTime(hFileB, &ftB, NULL, NULL);
	
	return CompareFileTime(&ftA, &ftB);
}
//________________________________________________________________
void UpdateImageBuffer(std::vector &inFiles){
	
	//sort them by creation date
	std::sort(allFiles.begin(), allFiles.end(), sortOnDate);
	
	//delete the oldest files that exceed the max buffer size
	for(unsigned int i=0; i<(allFiles.size()-MAXBUFFERSIZE); i++)
		allFiles.pop_back();
}

Recommended Answers

All 3 Replies

OMG that has to be an extremely sloooooow program. A more efficient way would be to keep the times in the vectorl so that they can easily be referenced, maybe something like this:

struct files
{
     std::string filename;
     FILETIME  tm;
};

vector<files> list;

line 18: are you sure that is how its coded in your program? std::vector &inFiles doesn't make any sense.

Thanks for your reply! Ok, ok, ok I have to agree with you that it wasn't exactly a high-speed application :D, but ey I'm just starting ;)

Anyway, below you can find my adjustments. Results:

1. No error
2. No sorting
3. No deleting when delete is called

Do you know if I get the right parameters from GetFileTime() to get the date that the file was created on? Suggestions?

struct Files{
	std::string filename;
	FILETIME tm;
};

bool sortOnDate(const Files& fA, const Files& fB){
	
	FILETIME ftA, ftB;
	ftA = fA.tm;
	ftB = fB.tm;
	return CompareFileTime(&ftA, &ftB)<0;
}

//________________________________________________________________
void UpdateImageBuffer(Halcon::HTuple &inFiles, int iNumFiles){

	std::vector<Files> allFiles;
	Files f;
	HANDLE hFile;
	FILETIME ft; 

	//put all the files on a vector
	for(int i = 0; i<iNumFiles-1; i++){

		f.filename = (std::string)inFiles[i];
		hFile = CreateFile((LPCWSTR)f.filename.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
		
		GetFileTime(hFile, &ft, NULL, NULL); 
		f.tm = ft; 

		allFiles.push_back(f);

		CloseHandle(hFile);
	}	
		
	//sort them by creation date
	std::sort(allFiles.begin(), allFiles.end(), sortOnDate);
	
	//delete the oldest files that exceed the max buffer size
	for(int i=0; i<iNumFiles-1; i++)
		if(i>MAXBUFFERSIZE)
			DeleteFile((LPCWSTR)allFiles[i].filename.c_str());

	
}

Found the solution!!

Remove the LPCWSTR cast and call the CreateFileA and DeleteFileA functions. This results in the correct sorting procedure and the correct deletion of the oldest files.

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.