How to write txt file?

How does these functions relate to writing file? Do you want to write to file in general or modify functions to accomplish that?

Simple approach – do it with PHP, for example with file_put_contents(). However between different hosting configurations this is not always reliable. Actually workflows in WP make real effort to not write any more files than really necessary – most of things go into database.

Complex and more reliable approach – Filesystem API.

I’d suggest considering database for storage first.