The previous example demonstrated reading and writing SMILES strings
from a file. This required the programmer to perform the file I/O
explicitly. Whilst this may be reasonable for SMILES strings that may
be read via STL's getline, it isn't suitable for more complex file
formats. To ease this task, OEChem provides the oemolstream
abstraction. The classes oemolistream and oemolostream allow input and
output of molecules using C++'s «
and »
operators
respectively.
#include "oechem.h" using namespace OEChem; int main() { oemolistream ims; oemolostream oms; OEMol mol; while (ims >> mol) oms << mol; return 0; }
In this example, the program will read molecules from cin (or stdin) in
SMILES format and write them to cout (or stdout) in (absolute) SMILES
format. Notice that in this example, there's no need to call the
mol.Clear()
method to reset the molecule,
OEAssignAromaticFlags()
to normalize aromaticity, or if(mol)
to test the validity of the molecule. This is done automatically for you
by the »
operator.
Some C++ programmers prefer--and C, Python and Java programmers need
to use--a functional interface rather than use the «
and
»
operators for file I/O. These are provided by the functions
OEReadMolecule and OEWriteMolecule that both take a oemolstream and
OEMolBase& as arguments. Identically, OEReadMolecule calls
mol.Clear()
and perceives appropriate properties for each
connection table, and skips any invalid molecules automatically.
#include "oechem.h" using namespace OEChem; int main() { oemolistream ims; oemolostream oms; OEMol mol; while (OEReadMolecule(ims,mol)) OEWriteMolecule(oms,mol); return 0; }