I am playing around with C++ and GDAL, making some basic exercises. And one of the things I wanted to build is to create a program that reads from a directory name the files within it, then in a foor loop it reads the dataset and writes out a the band into a new virtual raster. I managed to read from the directory the files within using the boost library. However when I wanted to create the raster it said that could not find the file name. I wanted to go around the problem by creating a new variable called bdir, and then concatenate the file's name, so I could pass it to the GDALOpenShared function.
Here is my code so you might have a better idea What I would like to accomplish:
#include <string>
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include "gdal/gdal.h"
#include "gdal/cpl_conv.h" /* for CPLMalloc() */
#include "gdal/vrtdataset.h"
#include <iostream>
#include <string>
#include "gdal/cpl_string.h"
#include <stdbool.h>
using namespace std;
using namespace boost::filesystem;
int main()
{
GDALAllRegister();
path p("/home/roger/Documents/09_Stack_Raster/media");
GDALDriver *poDriver = (GDALDriver*) GDALGetDriverByName( "VRT" );
GDALDataset *poSrcDS, *poVRTDS;
string bdir = "media/";
for (auto i = directory_iterator(p); i != directory_iterator(); i++)
{
string filename = i -> path().filename().string();
cout << filename << endl;
if (!is_directory(i->path()) && boost::ends_with(filename, ".tif"))
{
cout << i->path().filename().string() << endl;
std::string str = "media/" + i->path().filename().string().c_str();
poSrcDS = (GDALDataset *) GDALOpenShared(i->path().filename().string().c_str(), GA_ReadOnly);
poVRTDS = poDriver->CreateCopy("media/B02_stack.vrt", poSrcDS,FALSE,NULL,NULL,NULL);
GDALClose((GDALDatasetH) poSrcDS);
}
else
continue;
}
GDALClose((GDALDatasetH) poVRTDS);
}
I receive however the following error:
stack_raster.cpp: In function 'int main()':
stack_raster.cpp:33:40: error: invalid operands of types 'const char [7]' and 'const char*' to binary 'operator+'
std::string str = "media/" + i->path().filename().string().c_str();
Could someone help me here please? I checked this answer in Error: invalid operands of types ‘const char [35]’ and ‘const char [2]’ to binary ‘operator+’
and it is suggested that to be sure that string concatenation works in c++, both arguments should be string, which is something I did by declaring the bdir as string and adding the transformation from char to string by using .c_str() method in the path iterator.
Could someone give me some hints about it?
std::string str = "media/" + i->path().filename().string().c_str();
This does not compile because both of the operands to the +
operator are const char*
types [1] (sometimes known as "C strings"), not std::string
types.
Remove the final .c_str()
(which is unnecessary because you want to concatenate the std::string
, not the C string version) from the second operand and it should work.
[1] Technically the first operand is a const char [7]
type, which is not exactly the same as const char*
, but for the purposes of this question what matters is that these types cannot be concatenated using the +
operator.