Output file is blank


ROOT Version: 6.30/06


I want to print to a text file. I can print on the terminal but when I tried to print on the text file it is always blank. Please help.
Test.C (5.1 KB)

Hello @lum_totzin,
thanks for reaching out!

Can you be more precise about what you want to do? For example, what do you want to print?
I took a look at your macro, but it looks kinda incomplete…

Cheers, Monica

Dear mdessole

I want to print out the following onto a textfile:
outputFile << "IP: " << ip << ", Channel: " << nch << ", X: " << ch2Xtmp << ", Y: " << ch2Ytmp << ", ADC: " << nadc << std::endl; Now I can print this on the terminal but not onto a textfile, even though I included a check to see if it opens successfully(see line 77-78). Yes I include pary of the macro that I think is relevant otherwise it is very long.

Dear @lum_totzin,

your question doesn’t seem to be related to ROOT. If you think it is, can you please explain how?

Cheers,
Monica

It is working for me:

File test_cout.C

#include <fstream>
#include <iostream>

void test_cout() {
   std::ofstream outputFile("output.txt"); // create a new output file or overwrite an existing one

   if (outputFile.is_open()) { // check if the file was opened successfully
      outputFile << "Hello, world!\n"; // write data to the file
      outputFile.close(); // close the file when done
      std::cout << "Data was written to output.txt\n";
   } else {
      std::cerr << "Error opening file\n";
   }
}

script execution:

% root test_cout.C -q
   ------------------------------------------------------------------
  | Welcome to ROOT 6.33.01                        https://root.cern |
  | (c) 1995-2024, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for macosx64 on Jun 06 2024, 07:54:39                      |
  | From heads/master@v6-31-01-2264-g2a92722afe                      |
  | With Apple clang version 15.0.0 (clang-1500.3.9.4)               |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q'  |
   ------------------------------------------------------------------


Processing test_cout.C...
Data was written to output.txt
% cat output.txt 
Hello, world!
% 

Thanks @couet . I #include and it worked fine. Thank you very much!!