# Opening a TFile after initialization

**URL:** <https://root-forum.cern.ch/t/opening-a-tfile-after-initialization/53568>\
**Category:** Newbie\
**Created:** [February 10, 2023, 11:05am UTC](https://root-forum.cern.ch/t/opening-a-tfile-after-initialization/53568 "2023-02-10T11:05:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JackLindon](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@JackLindon](https://root-forum.cern.ch/u/JackLindon)\
**Post date:** [February 10, 2023, 11:05am UTC](https://root-forum.cern.ch/t/opening-a-tfile-after-initialization/53568/1 "2023-02-10T11:05:16Z")

</div>

I need to open a TFile after it is already initialized.

I thought I could just do:  
TFile f\_file;  
f\_file.Open(fileName.c\_str());

However this doesn’t work, testing

```auto
TFile f_file(fileName.c_str());
std::cout << f_file.IsOpen() << std::endl;

```

returns 1

and

```auto
TFile f_file;
f_file.Open(fileName.c_str());
std::cout << f_file.IsOpen() << std::endl;

```

returns 0.

I would expect these to return the exact same thing. What is wrong with this, and how do I actually open a TFile after initialization?

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [February 10, 2023, 5:35pm UTC](https://root-forum.cern.ch/t/opening-a-tfile-after-initialization/53568/2 "2023-02-10T17:35:55Z")

</div>

The best is to use

```auto
std::unique_ptr<TFile> f_file;
f_file.reset(TFile::Open(fileName.c_str());

```

Note `TFile::Open` is static function that allocate a new `TFile` based on the URL passed as argument. The actually type of the object will a class derived from `TFile` in the cases where the file not local (reading it from an `xrootd` server for example).

Using a `TFile` object instead of `TFile::Open` restrict to only reading local files.

You can not change the filename of a `TFile` after the original constructor.
