Using ROOT prompt commands in a C script

Hello, trying to write a root compiled C script to calculate how many files are in a premade root file. My current strategy is to

  1. Move into a directory using <directory name>->cd()
  2. Then count the number of contents in that directory, possibly using .ls and iterating over each line to get the total number of contents.

And append this number to a file. Is there an easier way of doing this or is this a good solution? Also how would I incorporate the root command line methods in a C script?

ROOT Version: 6.26/00
Platform: linuxx8664gcc
Compiler: g++ 9.3.0


One approach:

TFile* f = TFile::Open("your.root", "READ")
cout << f->GetListOfKeys()->GetSize() << endl;