# Problem opening .dat file

**URL:** <https://root-forum.cern.ch/t/problem-opening-dat-file/56582>\
**Category:** Newbie\
**Created:** [October 3, 2023, 12:21pm UTC](https://root-forum.cern.ch/t/problem-opening-dat-file/56582 "2023-10-03T12:21:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oscar2](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/oscar2/32/34640_2.png) [@Oscar2](https://root-forum.cern.ch/u/Oscar2)\
**Post date:** [October 3, 2023, 12:21pm UTC](https://root-forum.cern.ch/t/problem-opening-dat-file/56582/1 "2023-10-03T12:21:17Z")

</div>

So this is my first time using Root. I’ve installed everything and my professor gave me a script (.cpp) and a data file (.dat) just to test whether everything works fine.

I know there are no problems in neither of the files because they are working fine for the rest of my classmates. To keep it short, it reads all the data from the .dat file, builds a histogram and tries to fit it to a Poisson distribution.

However, whenever I run it I get the error “Warning in : Fit data is empty”. I think the problem happens when opening the file, because I added the following code after the open file instruction\_

```auto
FILE *f= fopen(filename.c_str(),"r");

if(f)
  {
      printf("Opened successfully \n");
  }
  else{
      printf("Couldn't open the file \n");
  }

```

and I always get the “Couldn’t open the file” text.

I reinstalled everything twice by following the steps and I’m clueless. The professor is too (he is no expert on the matter _sigh_).

Any help will be appreciated!

Additional info:  
\_ROOT Version: 6.28/06  
\_Platform: Windows 10  
\_Compiler: Visual Studio 2022 17.7.4

* * *

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [October 3, 2023, 12:35pm UTC](https://root-forum.cern.ch/t/problem-opening-dat-file/56582/2 "2023-10-03T12:35:52Z")

</div>

Well, we need more information. What is the value of `filename`? are you sure it is in the same directory than where you run your script? How and where are you starting ROOT?

---

<div class="post-metadata">

**Author:** ![Oscar2](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/oscar2/32/34640_2.png) [@Oscar2](https://root-forum.cern.ch/u/Oscar2)\
**Post date:** [October 3, 2023, 12:47pm UTC](https://root-forum.cern.ch/t/problem-opening-dat-file/56582/3 "2023-10-03T12:47:36Z")

</div>

Hi bellenot, thank you for answering!

The value of filename:

```auto
string filename = "fondo.dat";

```

Both files “fondo.dat” and “poisson.cpp” are in the same directory (C:\root\_v6.28.06\macros\Templates\_GM).

I’m not entirely sure about what you’re asking with the last question. Let me describe how I’m starting root:

1. Open x64 Native Tools Command Prompt for VS 2022 as Administrator.
2. Execute the following commands: `cd %USERPROFILE%` and then %C:\root\_v6.28.06\bin\thisroot.bat% so that .bat is running too.
3. Execute root by typing `root`.
4. Open the browser with `TBrowser b`.
5. Navigate the tabs until I find “poisson.cpp” (in the same folder as “fondo.dat”).
6. Click “Run”.

If it’s of any help, root always closes after executing the program. I have attached the whole program just in case, but there should be no need to check it because, as I said, it works fine for everyone except me.

Thank you again!

[poisson.cpp](https://root-forum.cern.ch/uploads/short-url/hOZSo0iC51bpFjej1NHKCYfFMsj.cpp) (5.5 KB)

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [October 3, 2023, 12:52pm UTC](https://root-forum.cern.ch/t/problem-opening-dat-file/56582/4 "2023-10-03T12:52:28Z")

</div>

OK, so that won’t work. The easiest way is the following:

1. Open x64 Native Tools Command Prompt for VS 2022 as Administrator.
2. Execute the following commands: `cd C:\root_v6.28.06\macros\Templates_GM` and then `C:\root_v6.28.06\bin\thisroot.bat` so that .bat is running too.
3. Execute root by typing `root`.
4. Open the browser with `TBrowser b`.
5. Click “Run”.

I.e. start from where the files are located

---

<div class="post-metadata">

**Author:** ![Oscar2](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/oscar2/32/34640_2.png) [@Oscar2](https://root-forum.cern.ch/u/Oscar2)\
**Post date:** [October 3, 2023, 12:58pm UTC](https://root-forum.cern.ch/t/problem-opening-dat-file/56582/5 "2023-10-03T12:58:34Z")

</div>

OMG bellenot thank you so much!  
I was so worried something was wrong! Now I can finally start learning with the Primer.

I take that the problem is that when changing to a different location I was somehow closing the “thisroot.bat” process? Or maybe that I was starting root from a location where I didn’t have the scripts I needed?

Thank you once again!

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [October 3, 2023, 1:02pm UTC](https://root-forum.cern.ch/t/problem-opening-dat-file/56582/6 "2023-10-03T13:02:25Z")

</div>

You’re very welcome! And FYI, the `thisroot.bat` script just sets the necessary environment variables allowing to run ROOT from anywhere. It doesn’t start any process. And if you want to run a macro from another directory, you have to cd to that directory or give the full path
