title: How to read data from AVRO file using C++ interface?
body: <p>I'm attempting to write a simple program to extract some data from a bunch of AVRO files.  The schema for each file may be different so I would like to read the files generically (i.e. without having to pregenerate and then compile in the schema for each) using the C++ interface.</p>
  
  <p>I have been attempting to follow the <code>generic.cc</code> example but it assumes a separate schema where I would like to read the schema from each AVRO file.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;iostream&gt;
  
  #include "Compiler.hh"
  #include "DataFile.hh"
  #include "Decoder.hh"
  #include "Generic.hh"
  #include "Stream.hh"
  
  const std::string BOLD("\033[1m");
  const std::string ENDC("\033[0m");
  const std::string RED("\033[31m");
  const std::string YELLOW("\033[33m");
  
  int main(int argc, char**argv)
  {
      std::cout &lt;&lt; "AVRO Test\n" &lt;&lt; std::endl;
  
      if (argc &lt; 2)
      {
          std::cerr &lt;&lt; BOLD &lt;&lt; RED &lt;&lt; "ERROR: " &lt;&lt; ENDC &lt;&lt; "please provide an "
                    &lt;&lt; "input file\n" &lt;&lt; std::endl;
          return -1;
      }
  
      avro::DataFileReaderBase dataFile(argv[1]);
      auto dataSchema = dataFile.dataSchema();
  
      // Write out data schema in JSON for grins
      std::ofstream output("data_schema.json");
      dataSchema.toJson(output);
      output.close();
  
      avro::DecoderPtr decoder = avro::binaryDecoder();
      auto inStream = avro::fileInputStream(argv[1]);
      decoder-&gt;init(*inStream);
  
      avro::GenericDatum datum(dataSchema);
      avro::decode(*decoder, datum);
      std::cout &lt;&lt; "Type: " &lt;&lt; datum.type() &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <p>Everytime I run the code, no matter what file I use, I get this:</p>
  
  <blockquote>
    <p>$ ./avrotest twitter.avro<br>
    AVRO Test</p>
    
    <p>terminate called after throwing an instance of 'avro::Exception'<br>
    what():  Cannot have negative length: -40 Aborted</p>
  </blockquote>
  
  <p>In addition to my own data files, I have tried using the data files located here: <a href="https://github.com/miguno/avro-cli-examples" rel="nofollow noreferrer">https://github.com/miguno/avro-cli-examples</a>, with the same result.</p>
  
  <p>I tried using the  <code>avrocat</code> utility on all of the same files and it works fine. What am I doing wrong?</p>
  
  <p>(NOTE: outputting the data schema for each file in JSON works correctly as expected)</p>