# Update file names with ImageWriter

**URL:** https://discourse.vtk.org/t/update-file-names-with-imagewriter/1955
**Category:** Development
**Created:** [October 21, 2019, 11:08am UTC](https://discourse.vtk.org/t/update-file-names-with-imagewriter/1955 "2019-10-21T11:08:36Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![1111](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/1/71e660/32.png) [@1111](https://discourse.vtk.org/u/1111)
#### Post date: [October 21, 2019, 11:08am UTC](https://discourse.vtk.org/t/update-file-names-with-imagewriter/1955/1 "2019-10-21T11:08:37Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [October 21, 2019, 12:12pm UTC](https://discourse.vtk.org/t/update-file-names-with-imagewriter/1955/2 "2019-10-21T12:12:06Z")

</div>

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

---

<div class="post-metadata">

### Author: ![1111](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/1/71e660/32.png) [@1111](https://discourse.vtk.org/u/1111)
#### Post date: [October 24, 2019, 4:56am UTC](https://discourse.vtk.org/t/update-file-names-with-imagewriter/1955/3 "2019-10-24T04:56:51Z")

</div>

Thank you for your reply.

This is my mistake.  
I’m sorry.
