Oddity with printf

Greetings all,

I have incorporated the code snippet that Axel linked to in his response to one of my earlier posts into one of my scripts and I have encountered what appears to be an oddity with printf.

The short script to illustrate the problem is as follows:


using namespace std;

void printf_oddity(){
string datafile;
ifstream filelist (“Input_files.txt”);
if (filelist.is_open())
while (! filelist.eof() )
getline (filelist,datafile);
cout << datafile << endl;

else cout << “Unable to open file”;

string fname1=datafile+".root";
cout << fname1 << endl;

printf(" Filename = %s \n",datafile);
printf(" Filename = %s \n",fname1);

The output I get from root when the script is run in interpreted form is as follows:

[quote]root [0] .x printf_oddity.C

Filename = ░°K☺ru980309_r01
Filename = ↑¢1☻↑¢1☻_r01.root[/quote]

If I try to compile the script I get a seg-fault.

The file “Input_files.txt” just contains the string “ru980309_r01”.

The string “datafile” seems to be read correctly and the string “fname1” seems to be created correctly - but they are interacting strangely with the printf command.

Any guidance on why this is happening and how I can correct it would be greatly appreciated.

Problem found in

ROOT 5.24/00 (trunk@29257, Jun 30 2009, 09:23:51 on win32)
CINT/ROOT C/C++ Interpreter version 5.17.00, Dec 21, 2008

and in

ROOT 5.17/04 (trunk@20359, Nov 16 2007, 19:13:00 on linux)
CINT/ROOT C/C++ Interpreter version 5.16.26, Oct 11, 2007


printf is a C function tht does not now the C++ type string. Simply do
printf(" Filename = %s \n",datafile.str());
instead of
printf(" Filename = %s \n",datafile);

the string::str function returns a const char* that print knows about.


Dear Rene,

When I make the suggested change to the script

printf(" Filename = %s \n",datafile.str()); printf(" Filename = %s \n",fname1.str());

I get the following output

[quote]Error: Can’t call string::str() in current scope C:\Fit_test_folder_ru98\printf_oddity.C(26)
Possible candidates are…
*** Interpreter error recovered ***[/quote]

So I still seem to be doing something wrong

sorry, my mistake, use datfile.c_str() instead of datafile.str()


Dear Rene,

Thank you - all working perfectly now.