0

I'm jira. I try to implement babel in c++ in order to convert a file of chemical structures in 'mol' format to that in 'smiles' format. I wrote the code similar to the one provided in documentation of openbabel ( http://openbabel.org/docs/dev/UseTheLibrary/CppExamples.html ) as follows.

#include <openbabel/obconversion.h>
#include <fstream>

int main(int argc, char **argv){
  std::ifstream ifs("a.mol");
  std::ofstream ofs("a.smi");

  OpenBabel::OBConversion conv(&ifs, &ofs);

  conv.setInAndOutFormats("MOL","SMI");

  int n = conv.Convert();
  std::out << n << " molecules are converted.\n"

  return 1;
}

but it always prints

0 molecules are converted.

and return an empty file "a.smi".

However, when I implement babel at the command line, e.g.

babel -imol a.mol -osmi

it works fine.

I would like ask if anyone has been experienced this kind of error and how you solved it. Because I follows all the things provided in documentation, I cannot figure out the reason of this error.

Thank you very much in advance.

Jira

Jira
  • 1
  • 1
  • 1
    Hi Jira, I'm Target Process... :D (couldn't resist...) – Roberto Dec 03 '15 at 15:05
  • 1
    Err... doesn't the `OBConversion` class need to be told *what* to do with those streams before you call `Convert()`? The example you linked calls [`SetInAndOutFormats()`](http://openbabel.org/api/2.3/classOpenBabel_1_1OBConversion.shtml#a4c751472f8bdb864b85cce430d42cc55) first. The command line specifies `-ismi` and `-omol`. – DevSolar Dec 03 '15 at 15:08
  • Hi DevSolar Thank you very much for suggesting that point. I already have conv.setInAndOutFormats("MOL","SMI"); in my code but forgot to type it in the question. Sorry for my mistake in the question. – Jira Dec 04 '15 at 06:40

0 Answers0