Trouble Opening Files in Windows Beta Version


Hello,

I am trying to read data from a simple CSV file and am able to do it successfully with a g++ compiler using C++ on Windows as well as ROOT (C++) on MacOS, but not ROOT on Windows.

The file is in the same folder in which I have my macros for ROOT but it cannot open and read data from the file. I can also see the data file in the directory with the ROOT terminal.

This code worked on MacOS and g++ and will successfully allow me to read and output data.
Here are the specific lines for opening the file.

ifstream inFile;
inFile.open(“filename.csv”);
inFile >> data;

When I run this on ROOT Windows, nothing is read from the file and it appears that the file is not being opened at all since nothing inside my if(inFile) statement is running. It instead outputs what I believe are the memory addresses of my data array since they were not filled with data.

So far I have tried the following fixes to no avail:
Adding the file path to the inFile.open("/folder/filename.csv"); or
inFile.open(“C::/root/folder/filename.csv”);
Changing the file to a .txt file and trying everything again

The file is a simple two-column.csv with integers.

This is my first time trying to read in a file using this version of ROOT, so I think there is likely some simple syntax I am missing when opening the file since the code works fine with other compilers.

I appreciate any help, thanks!
Kaitlyn

ROOT Version: 6.24.00 Windows Beta
Platform: Windows 10
Compiler: Visual Studio 2019


Welcome to the ROOT forum!
I just tried this:

void test_ifstream()
{
   float xval, yval;
   std::ifstream in;
   in.open("data.csv", std::ios::binary);
   if (!in.is_open()) {
      std::cerr << "failed to open data.csv\n";
      return;
   }
   while (in.is_open() && !in.eof()) {
      in >> xval >> yval;
      if (!in.good())
         break;
      std::cout << "xval: " << xval << " yval: " << yval << "\n";
   }
   in.close();
}

And here is what I get:

**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.4.6
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86'

C:\Users\bellenot>build\release\bin\thisroot.bat

C:\Users\bellenot>cd rootdev

C:\Users\bellenot\rootdev>root -l test_ifstream.cxx
root [0]
Processing test_ifstream.cxx...
xval: 1.07151 yval: 1.10427
xval: 1.1648 yval: 1.09762
xval: 1.22311 yval: 1.09641
xval: 1.35138 yval: 1.10125
xval: 1.44467 yval: 1.11102
[...]
xval: 9.81108 yval: 0.360761
xval: 10.118 yval: 0.361695
root [1]

Can you try?

I tried this test code on my data file in both .csv and .txt. and it output the “failed to open” statement.
It didn’t give me any compiler errors though, but neither does my code.

This is how I have tried to run it which works for my other macros.
image

Did you change the file name to match your own? How did you start ROOT?

Yes and I tried several different files as well.

I start ROOT using the system shortcut it created when I installed it. It automatically opens into the ROOT folder which is where I put my macros and data.

BTW, on Windows, if you want to give the full path, you have to use backslashes. For example:

   in.open("\\Users\\bellenot\\rootdev\\data\\data.csv", std::ios::binary);

Can you try this way?

When I change it to this it runs but gives no output which is a change I guess.

in.open("C:\\root_v6.24.00\\macros\\133Baformated.csv", std::ios::binary);

I’m not sure if that is the right way to write the path with the C::

Yes, that’s fine on Windows. Can you share your csv file (or an extract)?

The website won’t let me upload a .csv file but this is the .txt version.

133Baformated2.txt (34.1 KB)

void test_ifstream()
{
   int xval, yval;
   char c;
   std::ifstream in;
   in.open("\\Users\\bellenot\\rootdev\\data\\133Baformated2.csv", std::ios::binary);
   if (!in.is_open()) {
      std::cerr << "failed to open data.csv\n";
      return;
   }
   while (in.is_open() && !in.eof()) {
      in >> xval >> c >> yval;
      if (!in.good())
         break;
      std::cout << "xval: " << xval << " yval: " << yval << "\n";
   }
   in.close();
}

gives:

C:\Users\bellenot\rootdev>root -l test_ifstream.cxx
root [0]
Processing test_ifstream.cxx...
xval: 0 yval: 0
xval: 1 yval: 0
xval: 2 yval: 0
xval: 3 yval: 0
xval: 4 yval: 0
xval: 5 yval: 0
[...]
xval: 4092 yval: 1
xval: 4093 yval: 4
xval: 4094 yval: 2
xval: 4095 yval: 2
root [1]

So I tried removing this statement because it seemed like it was hitting the break statement. I left the while loop and everything else.

      if (!in.good())
         break;

It ended up giving the correct output for the first time!

xval: 0 yval: 0
xval: 1 yval: 0
xval: 2 yval: 0
xval: 3 yval: 0
xval: 4 yval: 0
xval: 5 yval: 0
[...]
xval: 4092 yval: 1
xval: 4093 yval: 4
xval: 4094 yval: 2
xval: 4095 yval: 2
xval: 4095 yval: 2

Now let me see if I can recreate the same thing on my other code…

It works perfectly!! Here’s the wonderful histogram it made :slight_smile:
image

The two major changes I made were the file path and the while loop.

Thank you so much!!

1 Like

Cool! Congratulations! And you’re most welcome!

1 Like