Write .txt file by TFile


Please read tips for efficient and successful posting and posting code

Hi,
I want to create a .txt file, then write a number to it using TFile. How can I do this?

Thanks in Advance,

_ROOT Version:6.22
Platform: Not Provided
_Compiler


File does not deal with .txt file. Why to you need File to write in a txt file ? C/C++ provide all the tools to write in a txt file.

Because I’m going to use the file in proof, then I think so just file created by TFile would be compatible with proof. For example, I have these codes:

fProofFile = new TProofOutputFile("file.txt",
                                TProofOutputFile::kMerge, opt, option);
fFile = fProofFile->OpenFile("RECREATE");

Maybe there is a way to use C/C++ tools in compatible way with proof, but I don’t know.

As it is a PROOF question I think the best person to answer is @ganis.

IIRC, one can use a TFile like a vanilla Unix file descriptor when open with ?filetype=raw:

 TFile and its remote access plugins can also be used to open any
 file, i.e. also non ROOT files, using:
    file.tar?filetype=raw
 This is convenient because the many remote file access plugins allow
 easy access to/from the many different mass storage systems.

ex: /head/athena/Tools/PyUtils/python/AthFileLite.py

I can open .txt file with Tfile but not write. I concluded that is not possible. Am I wrong?

you can:

$> root
   ------------------------------------------------------------------
  | Welcome to ROOT 6.24/06                        https://root.cern |
  | (c) 1995-2021, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Sep 02 2021, 14:20:23                 |
  | From tags/v6-24-06@v6-24-06                                      |
  | With c++ (GCC) 11.1.0                                            |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'       |
   ------------------------------------------------------------------

root [0] auto f = TFile::Open("foo.txt?filetype=raw", "RECREATE");
root [1] #include <stdio.h>
root [2] #include <string.h>
root [3] auto data = "some data\n";
root [4] f->WriteBuffer(data, strlen(data));
root [5] auto buf = "other data\n";
root [6] write(f->GetFd(), buf, strlen(buf));
root [7] f->Flush()
root [8] f->Close()
root [9] .q

$> cat foo.txt
some data
other data

thanks sbinet