Help with a piece of code

I run it here first, maybe you see something wrong or that could be done in another way.

The following piece of code is driving my HDD nuts. Particularly HDD keeps doing its own thing and nothing save for a complete shutdown will make the pc boot again. (tried restart only and HDD was still “rotating” and failed booting) (did sans checked SMART everything ok)

Basically I want to convert pcx to bmp. sometimes it runs ok. Sometimes I get corrupted .bmps and the strange HDD stuck in a loop problem.

Can someone take a look at the code?

// converts all pcx files into bmp
void convertPcxToBmp(std::string dataPath)
{
	namespace fs = boost::filesystem;
	fs::directory_iterator end_iter;

	for ( fs::directory_iterator dir_itr( dataPath + "Images/" ); dir_itr != end_iter; ++dir_itr )
	{
		try
		{
			if ( fs::is_regular_file( dir_itr->status() ) )
			{
				//std::cout << dir_itr->path().filename() << "\n";
				std::string filename = dir_itr->path().filename().string();
				filename = boost::locale::to_lower(filename);

				if(filename.find(".pcx") != std::string::npos)
				{
					SDL_Surface *bitmap;
					
					if (!(bitmap = BitmapHandler::loadBitmapFromDir("DATA/", filename)))
						logGlobal->errorStream()<<"Error: Failed to find file " << filename;
					
					fs::remove( dataPath + "Images/" + filename);
					filename = dataPath + "Images/" + removeExtension(filename) + ".bmp";
	 				SDL_SaveBMP(bitmap, filename.c_str());
					
				}
			}
			else
			{
				std::cout << dir_itr->path().filename() << " [other]\n";
			}
		}
		catch ( const std::exception & ex )
		{
			std::cout << dir_itr->path().filename() << " " << ex.what() << std::endl;
		}
	}
}

At first sight, this is very bad thing, that no return code of SDL_SaveBMP is analized. Maybe this function works not very good.
But I think it’s bad idea to remove file (“fs::remove( dataPath + “Images/” + filename);”)before ever running save to BMP or/and analizing return code.

PS Maybe your HDD is reacting to big number of very small files removed? If they are scattered on HDD, it can produce sounds while searching them

Code is OK. This sounds too much like hardware fault. Try coping large number of files across partitions (e.g. from C: to D: ) - it is very likely that you’ll encounter the same problem.

Checking SDL_SaveBMP return status is not enough - there is function called SDL_Error() or something like that that will give you string with description on what’s wrong. Add this check and run code until you’ll get those corrupted bmp’s.

Side note - I think SDL_Image offers SDL_SavePNG function. Try using it instead of saving to BMP. png’s have quite good compression so using png’s will give you smaller load on HDD.

I do that quite often and never encountered such problems. I have tried to add waits after each remove / write… no success.

I have not found any SDL native support for writing images as PNG. If you think it makes sense to save those .pcx to PNG can you please recommend a library to use? Something from here ?

This is certainly hardware issue. SMART won’t detect anything for now because SMART is purely statistical - if all parameters are below acceptable maximum it won’t report any failures.

I though that saving to png was somewhere in SDL_Image.
Try this one - encelo.netsons.org/old/sdl/
Consists from header + c file, needs libpng. Or at least should give you an idea on ho to add this.

Looks like it really was HDD :(.