HI.

I'm new to this forum and new to C++ programming so be kind !!

I've got a program that reads in a batch load of tif files works on them one at a time and then outputs the files as .png. Problem l have is that the tif filenames are basically filename.0001.tif - this is read in and saved to disk as a png file. But the filename changes to filename.0001.tif.png . How can l get rid of the .tif section and replace with .png.

The code uses argv[1] to hold the loaded filename - in this case filename.0001.tif. I am then doing ;-

std::string op=argv[1];
op+=".png";
write_png(op.c_str(), dst);

I'm basically adding the .png extension to the end of the original filename to use for saving out. I need to get rid of the .tif bit and replace with the .png bit.

Does anyone know how l can remove the .tif bit ???

Steviebax.

Recommended Answers

All 4 Replies

Use a combination of find_last_of(to get the last period) and substr(to grab everything up until that point) and then append the .png to the substring.
Or if you know your filenames are always going to be a fixed length, just overwrite the last 3 letters with "png" (op[15] = 'p' etc).

Use a combination of find_last_of(to get the last period) and substr(to grab everything up until that point) and then append the .png to the substring.
Or if you know your filenames are always going to be a fixed length, just overwrite the last 3 letters with "png" (op[15] = 'p' etc).

Thanks for replying.

I'm not that clued up with C++. The length of the filename would change so l can't assume a fixed length. Can you give me an example of how to use ' find last of ' and 'substr ' in this ???

I'd appreciate it.

int index = op.find_last_of('.'); //search from the back of the string
op = op.substr(0,index);  //take the first #index characters (doesn't include period)
                    //could do substr(0,index+1) to keep period and append
                     //"png" instead of ".png"
int index = op.find_last_of('.'); //search from the back of the string
op = op.substr(0,index);  //take the first #index characters (doesn't include period)
                    //could do substr(0,index+1) to keep period and append
                     //"png" instead of ".png"

Hi all.

I've managed to sort it now with :-

size_t found;
std::string op=argv[1];
found=op.find_last_of(".");
op=op.substr(0,found)+=".png";
write_png(op.c_str(), dst);

Thanks for all you help l wouldn't of been able to sort it so quickly without your advice.

Cheers.

Steviebax

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.