Update file names with ImageWriter

Hi,
I created a program to save images by key operation.

The current problem is to be overwritten because the names saved by keyspress are the same.

Every time I press the key, I want to save the file as:
slice_1_512x512.raw
slice_2_512x512.raw
slice_3_512x512.raw

I want to update a file with a variable in the file name.
But I don’t know how.
Can you tell me if you know?

Below is my code.

virtual void OnKeyPress() override
{

// Get the keypress
vtkRenderWindowInteractor *rwi = this->Interactor;
std::string key = rwi->GetKeySym();

// Output the key that was pressed
std::cout << "Pressed " << key << std::endl;

// Handle a "normal" key
if(key == "c")
{
  std::cout << "The a key was pressed." << std::endl;

  // adapt path !
  std::string filePathRaw ("slice_512x512.raw");
 
  vtkImageReslice *reslice = this->Reslice;
  
  vtkSmartPointer<vtkImageCast> castFilter = 
   vtkSmartPointer<vtkImageCast>::New();
  castFilter->SetOutputScalarTypeToUnsignedChar();
  castFilter->SetInputConnection(reslice->GetOutputPort());
  castFilter->Update();

  vtkSmartPointer<vtkImageWriter> writer =
   vtkSmartPointer<vtkImageWriter>::New();
  writer->SetInputConnection(castFilter->GetOutputPort());
  writer->SetFileName(filePathRaw.c_str());
  writer->Write();
}
// Forward events
vtkInteractorStyleTrackballCamera::OnKeyPress();
}

@dgobbi

I’m sorry, but this question is not a VTK question.

Thank you for your reply.

This is my mistake.
I’m sorry.