Problem with fprintf in root

Hi everybody,

I am using root:

 ------------------------------------------------------------------
  | Welcome to ROOT 6.26/06                         |
  | (c) 1995-2021, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for win64 on Jul 28 2022, 18:08:51                         |
  | From tags/v6-26-06@v6-26-06                                      |
  | With MSVC 19.32.31332.0                                          |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'       |
   ------------------------------------------------------------------

and I have the following function:

void test() {
  FILE *fid = fopen("testfile.txt", "wt");
  if (fid != NULL) {
    fprintf(fid, "text\n");
    //fprintf(fid, " %d \n", 1);
    fclose(fid);
  } else {
    printf("File is not created.\n");
  }
}

This function is working properly. But if I remove the comment mark // with intention to write a decimal number in the file (in this case, the decimal number is just 1), root terminates and file is empty. This happens in all case when I am trying to write any variable to the file.

Do you have some idea where is problem?

Vladimir

First, welcome to the ROOT Forum!
And I can reproduce the issue (on Windows 64 only). I’ll investigate, thanks for reporting it!

EDIT: BTW, could you open a Github issue for this (Issues · root-project/root · GitHub)?

On Mac it is fine:

void vladimir() {
  FILE *fid = fopen("testfile.txt", "wt");
  if (fid != NULL) {
    fprintf(fid, "text\n");
    fprintf(fid, " %d \n", 1);
    fclose(fid);
  } else {
    printf("File is not created.\n");
  }
  gSystem->Exec("cat testfile.txt");
}

gives:

% root vladimir.C
root [0] 
Processing vladimir.C...
text
 1 
root [1] 

I expect that code is fine. In my opinion, the problem must be in function fprintf. I was using such code for many years. But I have reinstalled root and MSVS recently. I am using now root v. 6.26/06. Which version of the root are you using?

Please see my answer above (@couet is working on MacOS)

Thanks. I am using Win64. I have created an issue on the github.

2 Likes

Thanks!

The issue on the GitHub has title: Problem with fprintf in root #11533.

1 Like

BTW, if it’s a blocker for you, you can replace fprintf by a more C++ version. For example:

   std::ofstream fid("testfile.txt");
   fid << "text\n";
   fid << 1 << "\n";

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.