diff --git a/examples/cpp/encode/file/main.cpp b/examples/cpp/encode/file/main.cpp
index 007899e..b8a4170 100644
--- a/examples/cpp/encode/file/main.cpp
+++ b/examples/cpp/encode/file/main.cpp
@@ -28,150 +28,134 @@
 #  include <config.h>
 #endif
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include <iostream>
+#include <fstream>
+#include <cstring>
 
 #define __STDC_FORMAT_MACROS
-#include <inttypes.h>
 
 #include "FLAC++/metadata.h"
 #include "FLAC++/encoder.h"
 
-#include <cstring>
-
 class OurEncoder: public FLAC::Encoder::File {
+	unsigned total_samples;
 public:
-	OurEncoder(): FLAC::Encoder::File() { }
+	OurEncoder(unsigned samples): FLAC::Encoder::File(), total_samples(samples) { }
 protected:
 	virtual void progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate);
 };
 
 #define READSIZE 1024
 
-static unsigned total_samples = 0; /* can use a 32-bit number due to WAVE size limitations */
-static FLAC__byte buffer[READSIZE/*samples*/ * 2/*bytes_per_sample*/ * 2/*channels*/]; /* we read the WAVE data into here */
-static FLAC__int32 pcm[READSIZE/*samples*/ * 2/*channels*/];
-
 int main(int argc, char *argv[])
 {
-	bool ok = true;
-	OurEncoder encoder;
-	FLAC__StreamEncoderInitStatus init_status;
-	FLAC__StreamMetadata *metadata[2];
-	FLAC__StreamMetadata_VorbisComment_Entry entry;
-	FILE *fin;
-	unsigned sample_rate = 0;
-	unsigned channels = 0;
-	unsigned bps = 0;
-
 	if(argc != 3) {
-		fprintf(stderr, "usage: %s infile.wav outfile.flac\n", argv[0]);
+		std::cerr << "usage: " << argv[0] << " infile.wav outfile.flac" << std::endl;
 		return 1;
 	}
 
-	if((fin = fopen(argv[1], "rb")) == NULL) {
-		fprintf(stderr, "ERROR: opening %s for output\n", argv[1]);
+	std::ifstream fin(argv[1]);
+	if(!fin.is_open()) {
+		std::cerr << "ERROR: opening " << argv[1] << " for output" << std::endl;
 		return 1;
 	}
 
 	/* read wav header and validate it */
+	FLAC__byte buffer[READSIZE/*samples*/ * 2/*bytes_per_sample*/ * 2/*channels*/]; /* we read the WAVE data into here */
 	if(
-		fread(buffer, 1, 44, fin) != 44 ||
-		memcmp(buffer, "RIFF", 4) ||
-		memcmp(buffer+8, "WAVEfmt \020\000\000\000\001\000\002\000", 16) ||
-		memcmp(buffer+32, "\004\000\020\000data", 8)
+		!fin.read(reinterpret_cast<char *>(buffer), 44) ||
+		std::memcmp(buffer, "RIFF", 4) ||
+		std::memcmp(buffer+8, "WAVEfmt \020\000\000\000\001\000\002\000", 16) ||
+		std::memcmp(buffer+32, "\004\000\020\000data", 8)
 	) {
-		fprintf(stderr, "ERROR: invalid/unsupported WAVE file, only 16bps stereo WAVE in canonical form allowed\n");
-		fclose(fin);
-		return 1;
+		std::cerr << "ERROR: invalid/unsupported WAVE file, only 16bps stereo WAVE in canonical form allowed" << std::endl;
+	return 1;
 	}
-	sample_rate = ((((((unsigned)buffer[27] << 8) | buffer[26]) << 8) | buffer[25]) << 8) | buffer[24];
-	channels = 2;
-	bps = 16;
-	total_samples = (((((((unsigned)buffer[43] << 8) | buffer[42]) << 8) | buffer[41]) << 8) | buffer[40]) / 4;
+	unsigned sample_rate = (((((static_cast<unsigned>(buffer[27]) << 8) | buffer[26]) << 8) | buffer[25]) << 8) | buffer[24];
+	unsigned channels = 2;
+	unsigned bps = 16;
+	unsigned total_samples = ((((((static_cast<unsigned>(buffer[43]) << 8) | buffer[42]) << 8) | buffer[41]) << 8) | buffer[40]) / 4;
 
 	/* check the encoder */
+	OurEncoder encoder(total_samples);
 	if(!encoder) {
-		fprintf(stderr, "ERROR: allocating encoder\n");
-		fclose(fin);
+		std::cerr << "ERROR: allocating encoder" << std::endl;
 		return 1;
 	}
 
-	ok &= encoder.set_verify(true);
+	bool ok = encoder.set_verify(true);
 	ok &= encoder.set_compression_level(5);
 	ok &= encoder.set_channels(channels);
 	ok &= encoder.set_bits_per_sample(bps);
 	ok &= encoder.set_sample_rate(sample_rate);
 	ok &= encoder.set_total_samples_estimate(total_samples);
 
-	/* now add some metadata; we'll add some tags and a padding block */
-	if(ok) {
-		if(
-			(metadata[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)) == NULL ||
-			(metadata[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)) == NULL ||
-			/* there are many tag (vorbiscomment) functions but these are convenient for this particular use: */
-			!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ARTIST", "Some Artist") ||
-			!FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false) || /* copy=false: let metadata object take control of entry's allocated string */
-			!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "YEAR", "1984") ||
-			!FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false)
-		) {
-			fprintf(stderr, "ERROR: out of memory or tag error\n");
-			ok = false;
-		}
+	if(!ok) {
+		std::cerr << "ERROR: setting encoder options" << std::endl;
+		return 1;
+	}
 
-		metadata[1]->length = 1234; /* set the padding length */
+	/* now add some metadata; we'll add some tags and a padding block */
+	FLAC::Metadata::VorbisComment vorbis_comment;
+	FLAC::Metadata::Padding padding(1234);
+	if(
+		/* there are many tag (vorbiscomment) functions but these are convenient for this particular use: */
+		!vorbis_comment.append_comment(FLAC::Metadata::VorbisComment::Entry("ARTIST", "Some Artist")) || /* copy=false: let metadata object take control of entry's allocated string */
+		!vorbis_comment.append_comment(FLAC::Metadata::VorbisComment::Entry("YEAR", "1984")) ||
+		!vorbis_comment.is_valid() ||
+		!padding.is_valid()
+	) {
+		std::cerr << "ERROR: out of memory or tag error" << std::endl;
+		return 1;
+	}
 
-		ok = encoder.set_metadata(metadata, 2);
+	FLAC::Metadata::Prototype *metadata[2];
+	metadata[0] = &vorbis_comment;
+	metadata[1] = &padding;
+	if (!encoder.set_metadata(metadata, 2)) {
+		std::cerr << "ERROR: setting metadata failed" << std::endl;
+		return 1;
 	}
 
 	/* initialize encoder */
-	if(ok) {
-		init_status = encoder.init(argv[2]);
-		if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
-			fprintf(stderr, "ERROR: initializing encoder: %s\n", FLAC__StreamEncoderInitStatusString[init_status]);
-			ok = false;
-		}
+	FLAC__StreamEncoderInitStatus init_status = encoder.init(argv[2]);
+	if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
+		std::cerr << "ERROR: initializing encoder: " << FLAC__StreamEncoderInitStatusString[init_status] << std::endl;
+		return 1;
 	}
 
 	/* read blocks of samples from WAVE file and feed to encoder */
-	if(ok) {
-		size_t left = (size_t)total_samples;
-		while(ok && left) {
-			size_t need = (left>READSIZE? (size_t)READSIZE : (size_t)left);
-			if(fread(buffer, channels*(bps/8), need, fin) != need) {
-				fprintf(stderr, "ERROR: reading from WAVE file\n");
-				ok = false;
-			}
-			else {
-				/* convert the packed little-endian 16-bit PCM samples from WAVE into an interleaved FLAC__int32 buffer for libFLAC */
-				size_t i;
-				for(i = 0; i < need*channels; i++) {
-					/* inefficient but simple and works on big- or little-endian machines */
-					pcm[i] = (FLAC__int32)(((FLAC__int16)(FLAC__int8)buffer[2*i+1] << 8) | (FLAC__int16)buffer[2*i]);
-				}
-				/* feed samples to encoder */
-				ok = encoder.process_interleaved(pcm, need);
+	size_t left = static_cast<size_t>(total_samples);
+	FLAC__int32 pcm[READSIZE/*samples*/ * 2/*channels*/];
+	while(ok && left) {
+		size_t need = (left>READSIZE? static_cast<size_t>(READSIZE) :static_cast<size_t> (left));
+		if (!fin.read(reinterpret_cast<char *>(buffer), need*channels*(bps/8))) {
+			std::cerr << "ERROR: reading from WAVE file" << std::endl;
+			ok = false;
+		}
+		else {
+			/* convert the packed little-endian 16-bit PCM samples from WAVE into an interleaved FLAC__int32 buffer for libFLAC */
+			for(size_t i = 0; i < need*channels; ++i) {
+			/* inefficient but simple and works on big- or little-endian machines */
+			pcm[i] = static_cast<FLAC__int32>((static_cast<FLAC__int16>(static_cast<FLAC__int8>(buffer[2*i+1])) << 8) |
+			         static_cast<FLAC__int16>(buffer[2*i]));
 			}
-			left -= need;
+			/* feed samples to encoder */
+			ok = encoder.process_interleaved(pcm, need);
 		}
+		left -= need;
 	}
 
 	ok &= encoder.finish();
 
-	fprintf(stderr, "encoding: %s\n", ok? "succeeded" : "FAILED");
-	fprintf(stderr, "   state: %s\n", encoder.get_state().resolved_as_cstring(encoder));
-
-	/* now that encoding is finished, the metadata can be freed */
-	FLAC__metadata_object_delete(metadata[0]);
-	FLAC__metadata_object_delete(metadata[1]);
-
-	fclose(fin);
+	std::cerr << "encoding: " << (ok? "succeeded" : "FAILED") << std::endl;
+	std::cerr << "   state: " << encoder.get_state().resolved_as_cstring(encoder) << std::endl;
 
 	return 0;
 }
 
 void OurEncoder::progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate)
 {
-	fprintf(stderr, "wrote %" PRIu64 " bytes, %" PRIu64 "/%u samples, %u/%u frames\n", bytes_written, samples_written, total_samples, frames_written, total_frames_estimate);
+	std::cerr << "wrote " << bytes_written << " bytes, " << samples_written << "/" << total_samples << " samples, "
+	          << frames_written << "/" << total_frames_estimate << " frames" << std::endl;
 }
