This is easy to do using GDAL. Here is function I regularly use to convert raster data to XYZ format. Note: .xyz is just a space (or tab) delimited text file.
#include "gdal_priv.h"
void Raster::toXYZ(const char *rasterPath, const char *xyzPath)
{
setProperties(rasterPath);
GDALDataset *pRaster;
pRaster = (GDALDataset*) GDALOpen(rasterPath, GA_ReadOnly);
int nRows = pRaster.RasterYSize()
int nCols = pRaster.RasterXSize()
double noData = -9999.0
double xtlCenter, ytlCenter, xCenter, yCenter;
double transform[6];
transform = pRaster->GetGeoTransform(transform)
xtlCenter = transform[0] + (transform[1]/2.0);
ytlCenter = transform[3] + (fabs(transform[5])/2.0);
float *read = (float*) CPLMalloc(sizeof(float)*1);
QFile fout(xyzPath);
fout.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&fout);
out.setRealNumberNotation(QTextStream::FixedNotation);
for (int i=0; i<nRows; i++)
{
yCenter = ytlCenter + (i*fabs(transform[5]));
for (int j=0; j<nCols; j++)
{
pRaster->GetRasterBand(1)->RasterIO(GF_Read,j,i,1,1,read,1,1,GDT_Float32,0,0);
if (*read != noData)
{
xCenter = xtlCenter + (j*transform[1]);
out.setRealNumberPrecision(5);
out << xCenter << "\t" <<yCenter << "\t" << *read <<"\n";
}
}
}
fout.close();
CPLFree(read);
GDALClose(pRaster);
}