You can probably use the LaTeX2e-kernel-macros \@starttoc and \addtocontents{<file-extension>}{<tokens>}.
\addtocontents will write (unexpanded) to the aux-file some directives for unexpanded writing <tokens> into the file \jobname.<file-extension> in case that file is open for input when the aux-file is processed at the end of the LaTeX-run.
\@starttoc{<file-extension>} will make TeX read/process the file \jobname.<file-extension> in case that file exists and then destroy that file and then create it anew and open it for input so that when the aux-file is processed at the end of the LaTeX-run, that file is open for input.
In case you wish to do some more fancy things, have a look at the \addcontentsline-macro of the LaTeX2e-kernel.
Basiscally \addcontentsline{<file-extension>}{<command name>}{<tokens>} will write to the aux-file some directives for unexpanded writing the token-sequence \contentsline{<command name>}{<tokens>}{<page number>} into the file \jobname.<file-extension> in case that file is open for input when the aux-file is processed at the end of the LaTeX-run.
After having written \jobname.<file-extension> during processing the aux-file at the end of the LaTeX-run, you will find that directive
\contentsline{<command name>}{<tokens>}{<page number>} in it. Thus that directive will be carried out when -- due to \@starttoc -- the file \jobname.<file-extension> is read/processed.
\contentsline{<command name>}{<tokens>}{<page number>}in turn will call a macro \l@<command name> which processes two arguments, namely the <tokens>-argument and the <page number>-argument.
When this mechanism is used with the toc-file and sectioning-commands, <command name> will be something like section or subsection yielding the execution of the macros \l@section or \l@subsection but you can as well define your own \l@..-macros for your own categories of rubrification.
The \addtocontents...-\@starttoc{<file-extension>}-mechanism does beneath other things define a control-sequence \tf@<file-extension>. Therefore for finding out whether that mechanism has alrady allocated a \write-handle for writing a file \jobname.<file-extension>, you can use the \@ifundefined-macro in order to check whether the control-sequence \tf@<file-extension> is already defined.
But this does only work as long as the scrwfile-package is not in use -- by the way: You might be interested in the scrwfile-package in case you intend to create many lists like toc, lof or lot in your document. More information about that package can be found on CTAN: https://www.ctan.org/pkg/scrwfile
The LaTeX2e-kernel is explained and commented in the file source2e.pdf which is available at https://www.ctan.org/pkg/source2e .
The \addtocontents-\@starttoc-mechanism and the \addcontentsline-macro which makes use of that mechanism are explained in the section which is named File F ltsect.dtx → 59 Sectioning commands → 59.3 Table of Contents etc.
\documentclass{article}
\usepackage{hyperref}
\usepackage{verbatim}
\makeatletter
\newcommand\l@songline[2]{%
\par Now we have data about another song:\\
Title of song: #1. Song is printed on page: #2.%
}%
\makeatother
\begin{document}
\LaTeX{} will now write directives to aux-file for writing the first
line into \jobname.weird. These directives will be carried out at
the end of the \LaTeX-run when the aux-file is read/processed in
case at that time the file \jobname.weird is open for writing to it.\\
\addtocontents{weird}{First line in file \jobname.weird.}%
\bigskip
This is what \jobname.weird looks like before calling \verb|\@starttoc|: \\
\verbatiminput{\jobname.weird}
\bigskip
This is how \verb|\l@songline| is defined:\\{%
\csname verbatim@font\endcsname\selectfont
\expandafter\meaning\csname l@songline\endcsname
}%
\bigskip
The file \jobname.weird is \csname @ifundefined\endcsname{tf@weird}{not}{already} allocated.
\bigskip
\LaTeX will now read/process \jobname.weird and then destroy that file
and create it anew and open it for writing. Thus at the end of the
\LaTeX-run, when the aux-file is read/processed, that file will be open
for writing to it:\bigskip
\csname @starttoc\endcsname{weird}%
\bigskip
The file \jobname.weird is \csname @ifundefined\endcsname{tf@weird}{not}{already} allocated.
\bigskip
\LaTeX{} will now write directives to aux-file for writing the second
line into \jobname.weird. These directives will be carried out at
the end of the \LaTeX-run when the aux-file is read/processed in
case at that time the file \jobname.weird is open for writing to it.\\
\addtocontents{weird}{Second line in file \jobname.weird.}%
\bigskip
Now two \verb|\addcontentsline|-entries for writing things to \jobname.weird
that need to be "rubrified" by means of applying the \verb|l@songgline|-macro.
\addcontentsline{weird}{songline}{Morning has broken}
\addcontentsline{weird}{songline}{Final Countdown}
\end{document}
tocbasicwhich is part of the KOMA-Script bundle and already used by the KOMA-Script classes. – esdd Nov 15 '16 at 16:07