# Input string/int in a file name

**URL:** <https://root-forum.cern.ch/t/input-string-int-in-a-file-name/51186>\
**Category:** Newbie\
**Created:** [August 14, 2022, 4:14pm UTC](https://root-forum.cern.ch/t/input-string-int-in-a-file-name/51186 "2022-08-14T16:14:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![robygru](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@robygru](https://root-forum.cern.ch/u/robygru)\
**Post date:** [August 14, 2022, 4:14pm UTC](https://root-forum.cern.ch/t/input-string-int-in-a-file-name/51186/1 "2022-08-14T16:14:10Z")

</div>

Hi, is there a way to put an input value into a SaveAs file name?  
I have lot of saveas and i don’t want to change manually all the name of the file for each canvas i save.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [August 14, 2022, 4:26pm UTC](https://root-forum.cern.ch/t/input-string-int-in-a-file-name/51186/2 "2022-08-14T16:26:09Z")

</div>

Use [TString::Form](https://root.cern/doc/master/classTString.html#aeb44d6183d8b1f1b7090dbd3c93f5e39), with the same syntax as printf; e.g. supposing x is an integer:

```auto
canvas->SaveAs(Form("%s%d%s","image",x,".png"));

```

---

<div class="post-metadata">

**Author:** ![robygru](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@robygru](https://root-forum.cern.ch/u/robygru)\
**Post date:** [August 14, 2022, 4:34pm UTC](https://root-forum.cern.ch/t/input-string-int-in-a-file-name/51186/3 "2022-08-14T16:34:51Z")

</div>

i have an error

> error: too many arguments to function call, expected at most 2, have 4

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [August 14, 2022, 4:41pm UTC](https://root-forum.cern.ch/t/input-string-int-in-a-file-name/51186/4 "2022-08-14T16:41:58Z")

</div>

Sorry, I omitted Form; corrected now.

---

<div class="post-metadata">

**Author:** ![robygru](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@robygru](https://root-forum.cern.ch/u/robygru)\
**Post date:** [August 14, 2022, 5:03pm UTC](https://root-forum.cern.ch/t/input-string-int-in-a-file-name/51186/5 "2022-08-14T17:03:38Z")

</div>

now it works!  
If i want to put a string instead, is it possible?

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [August 14, 2022, 7:07pm UTC](https://root-forum.cern.ch/t/input-string-int-in-a-file-name/51186/6 "2022-08-14T19:07:00Z")

</div>

> [@robygru](#):
>
> If i want to put a string instead, is it possible?

Absolutely:

```c++
std::string aa = "abcd"; // your string
TCanvas *c1 = new TCanvas("c1", "My canvas", 0, 0, 600, 480); // an empty canvas
c1->SaveAs(Form("CanvasTitled_%s.png", aa.c_str())); // %s will be replaced with abcd

```

gives

```auto
Info in <TCanvas::Print>: file CanvasTitled_abcd.png has been created

```

---

<div class="post-metadata">

**Author:** ![robygru](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@robygru](https://root-forum.cern.ch/u/robygru)\
**Post date:** [August 14, 2022, 7:32pm UTC](https://root-forum.cern.ch/t/input-string-int-in-a-file-name/51186/7 "2022-08-14T19:32:33Z")

</div>

thank you all for your help!!
