# How to write a file in Cling non-interactive mode

**URL:** <https://root-forum.cern.ch/t/how-to-write-a-file-in-cling-non-interactive-mode/50380>\
**Category:** Newbie\
**Tags:** cling\
**Created:** [June 13, 2022, 12:44pm UTC](https://root-forum.cern.ch/t/how-to-write-a-file-in-cling-non-interactive-mode/50380 "2022-06-13T12:44:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![geneweber](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/geneweber/32/30005_2.png) [@geneweber](https://root-forum.cern.ch/u/geneweber)\
**Post date:** [June 13, 2022, 12:44pm UTC](https://root-forum.cern.ch/t/how-to-write-a-file-in-cling-non-interactive-mode/50380/1 "2022-06-13T12:44:02Z")

</div>

_Cling Version:_ 1.0 dev  
_Platform:_ Ubuntu Linux 20.04.1  
_Compiler:_ gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0

* * *

Hi,  
I’d like to be able to do file io running Cling in non-interactive mode.

Running interactively, given the file test3.cpp which contains:

```cpp
#include <iostream>
#include <fstream>
#include <string>

// Function that returns the square of the number.
int sq_num(int num) {
    return num * num;
}

// Write to file
void write_file(std::string file_name, int num) {

    FILE* out_file = fopen(file_name.c_str(), "w");
    if(!out_file) {
        std::cerr << "\nCannot open file named: " << file_name << ".\n\n";
        exit (EXIT_FAILURE);
    }

    fprintf(out_file, "Number = %d", num);
    fclose(out_file);
}

void test3() {
    int i = sq_num(11);
    write_file("foobar.txt", i);
}

```

Cling creates the file foobar.txt containing “Number = 121”.

```nohighlight
******************CLING******************
* Type C++ code and press enter to run it *
* Type .q to exit *
*******************************************
[cling]$ .x test3.cpp
[cling]$ .q

```

But if I add “#! test3.cpp” as the first line of test3.cpp and run it non-interactively:

```nohighlight
$ ../obj/bin/cling test3.cpp

```

There are no errors or warnings, but the foobar.txt file is not created. Is there an option or setting that would allow this to work?

Thanks,

Gene

---

<div class="post-metadata">

**Author:** ![vvassilev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/f0a364/32.png) [@vvassilev](https://root-forum.cern.ch/u/vvassilev)\
**Post date:** [June 13, 2022, 5:42pm UTC](https://root-forum.cern.ch/t/how-to-write-a-file-in-cling-non-interactive-mode/50380/2 "2022-06-13T17:42:43Z")

</div>

Hi,

The shebang approach will work if you have a statement calling the function `test3`. Maybe in the end of the file add `test3();`

Best, Vassil

---

<div class="post-metadata">

**Author:** ![geneweber](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/geneweber/32/30005_2.png) [@geneweber](https://root-forum.cern.ch/u/geneweber)\
**Post date:** [June 13, 2022, 6:05pm UTC](https://root-forum.cern.ch/t/how-to-write-a-file-in-cling-non-interactive-mode/50380/3 "2022-06-13T18:05:04Z")

</div>

Yes, that worked. Thank you Vassil.
