Programming languages

How to fix C++ ‘fatal error: stringstream: No such file or directory’

Problem:

Your C++ code contains a line like

#include <stringstream>

since you want to use std::stringstream, but your compiler gives you this error message:

main.cpp:2:10: fatal error: stringstream: No such file or directory
 #include <stringstream>
          ^~~~~~~~~~~~~~
compilation terminated.

Solution:

The header is called sstream, not stringstream! Use this #include directive instead:

#include <sstream>
Posted by Uli Köhler in C/C++, GCC errors

How to parse hex strings in C++ using std::stringstream

This function converts any hex string like 55 to its equivalent decimal value:

#include <sstream>
#include <int>

unsigned int hexToDec(const std::string& str) {
    unsigned int ret;
    std::stringstream ss;
    ss << std::hex << str;
    ss >> ret;
    return ret;
}

e.g.

hexToDec("55"); // returns 85

Note that while the code is somewhat concise, it might be neither the most performant nor the most concise option especially for developers that don’t really know std::stringstream.

Posted by Uli Köhler in C/C++

How to get first three characters of std::string in C++

To get the first three characters of myString use myString.substr() like this:

std::string firstTwo = myString.substr(0, 3);

The first argument ob substr() is the offset (i.e. how many characters to skip) which is 0 in this case since we don’t want to skip any characters.

The second argument of substr() is the number of characters to get. Since we want to get three characters, this is 3.

Posted by Uli Köhler in C/C++

How to get first two characters of std::string in C++

To get the first two characters of myString use myString.substr() like this:

std::string firstTwo = myString.substr(0, 2);

The first argument ob substr() is the offset (i.e. how many characters to skip) which is 0 in this case since we don’t want to skip any characters.

The second argument of substr() is the number of characters to get. Since we want to get two characters, this is 2.

Posted by Uli Köhler in C/C++

C++ std::string equivalent to Python’s rstrip

In Python you can do

"test ".rstrip()

whereas in C++ the easiest way is to use the the Boost String Algorithms library:

#include <boost/algorithm/string/trim.hpp>

using namespace boost::algorithm;

string to_trim = "test ";
string trimmed = trim_right_copy(to_trim);

_copy in trim_right_copy() means that the original string is not modified.

Full example:

#include <iostream>
#include <string>
#include <boost/algorithm/string/trim.hpp>

using namespace std;
using namespace boost::algorithm;

int main() {
    string to_trim = "test ";
    string trimmed = trim_right_copy(to_trim);
    cout << trimmed << endl; // Prints 'test'
}

In case you want to trim a custom set of characters, use trim_right_copy_if() together with is_any_of():

#include <iostream>
#include <string>
#include <boost/algorithm/string/trim.hpp>

using namespace std;
using namespace boost::algorithm;

int main() {
    string to_trim = "testXbbca";
    string trimmed = trim_right_copy_if(to_trim, is_any_of("abc"));
    cout << trimmed << endl; // Prints 'testX'
}

 

Posted by Uli Köhler in C/C++

C++ std::string equivalent to Python’s lstrip

In Python you can do

" test".lstrip()

whereas in C++ the easiest way is to use the the Boost String Algorithms library:

#include <boost/algorithm/string/trim.hpp>

using namespace boost::algorithm;

string to_trim = " test";
string trimmed = trim_left_copy(to_trim);

_copy in trim_left_copy() means that the original string is not modified.

Full example:

#include <iostream>
#include <string>
#include <boost/algorithm/string/trim.hpp>

using namespace std;
using namespace boost::algorithm;

int main() {
    string to_trim = " test";
    string trimmed = trim_left_copy(to_trim);
    cout << trimmed << endl; // Prints 'test'
}

In case you want to trim a custom set of characters, use trim_left_copy_if() together with is_any_of():

#include <iostream>
#include <string>
#include <boost/algorithm/string/trim.hpp>

using namespace std;
using namespace boost::algorithm;

int main() {
    string to_trim = "bbcaXtest";
    string trimmed = trim_left_copy_if(to_trim, is_any_of("abc"));
    cout << trimmed << endl; // Prints 'Xtest'
}

 

Posted by Uli Köhler in C/C++

How to recompress gzipped .gz files to .xz files on Linux

Copy and paste this shell function into your bash or zsh:

function gzToXz { zcat "$1" | xz -c - -ev9 > "${1%.*}.xz" ; }

This will decompress the file using zcat and pipe it into xz with the highest compression setting (-ev9). If you prefer less compression but more speed, use e.g. -5 instead of -ev9.

Use like this:

gzToXz my.gz

which will recompress my.gz to my.xz.

In case you want to recompress every .gz file in the current directory, use:

for i in *.gz ; do echo "$i" ; gzToXz "$i" ; done

It might be best to make a backup of the data even though I have used this script many times for my own data.

Posted by Uli Köhler in Linux, Shell

How to list available projections in pyproj

To get a list of all available projections for pyproj, use this code:

import pyproj
print(pyproj.pj_list)

For pyproj-2.2.0 on my Ubuntu 18.04 computer, this is the resulting list:

{
    'aea': 'Albers Equal Area',
    'aeqd': 'Azimuthal Equidistant',
    'affine': 'Affine transformation',
    'airy': 'Airy',
    'aitoff': 'Aitoff',
    'alsk': 'Mod. Stereographic of Alaska',
    'apian': 'Apian Globular I',
    'august': 'August Epicycloidal',
    'axisswap': 'Axis ordering',
    'bacon': 'Bacon Globular',
    'bertin1953': 'Bertin 1953',
    'bipc': 'Bipolar conic of western hemisphere',
    'boggs': 'Boggs Eumorphic',
    'bonne': 'Bonne (Werner lat_1=90)',
    'calcofi': 'Cal Coop Ocean Fish Invest Lines/Stations',
    'cart': 'Geodetic/cartesian conversions',
    'cass': 'Cassini',
    'cc': 'Central Cylindrical',
    'ccon': 'Central Conic',
    'cea': 'Equal Area Cylindrical',
    'chamb': 'Chamberlin Trimetric',
    'collg': 'Collignon',
    'comill': 'Compact Miller',
    'crast': 'Craster Parabolic (Putnins P4)',
    'deformation': 'Kinematic grid shift',
    'denoy': 'Denoyer Semi-Elliptical',
    'eck1': 'Eckert I',
    'eck2': 'Eckert II',
    'eck3': 'Eckert III',
    'eck4': 'Eckert IV',
    'eck5': 'Eckert V',
    'eck6': 'Eckert VI',
    'eqearth': 'Equal Earth',
    'eqc': 'Equidistant Cylindrical (Plate Carree)',
    'eqdc': 'Equidistant Conic',
    'euler': 'Euler',
    'etmerc': 'Extended Transverse Mercator',
    'fahey': 'Fahey',
    'fouc': 'Foucaut',
    'fouc_s': 'Foucaut Sinusoidal',
    'gall': 'Gall (Gall Stereographic)',
    'geoc': 'Geocentric Latitude',
    'geocent': 'Geocentric',
    'geogoffset': 'Geographic Offset',
    'geos': 'Geostationary Satellite View',
    'gins8': 'Ginsburg VIII (TsNIIGAiK)',
    'gn_sinu': 'General Sinusoidal Series',
    'gnom': 'Gnomonic',
    'goode': 'Goode Homolosine',
    'gs48': 'Mod. Stereographic of 48 U.S.',
    'gs50': 'Mod. Stereographic of 50 U.S.',
    'hammer': 'Hammer & Eckert-Greifendorff',
    'hatano': 'Hatano Asymmetrical Equal Area',
    'healpix': 'HEAPJ_LPix',
    'rhealpix': 'rHEAPJ_LPix',
    'helmert': '3(6)-, 4(8)- and 7(14)-parameter Helmert shift',
    'hgridshift': 'Horizontal grid shift',
    'horner': 'Horner polynomial evaluation',
    'igh': 'Interrupted Goode Homolosine',
    'imw_p': 'International Map of the World Polyconic',
    'isea': 'Icosahedral Snyder Equal Area',
    'kav5': 'Kavraisky V',
    'kav7': 'Kavraisky VII',
    'krovak': 'Krovak',
    'labrd': 'Laborde',
    'laea': 'Lambert Azimuthal Equal Area',
    'lagrng': 'Lagrange',
    'larr': 'Larrivee',
    'lask': 'Laskowski',
    'lonlat': 'Lat/long (Geodetic)',
    'latlon': 'Lat/long (Geodetic alias)',
    'latlong': 'Lat/long (Geodetic alias)',
    'longlat': 'Lat/long (Geodetic alias)',
    'lcc': 'Lambert Conformal Conic',
    'lcca': 'Lambert Conformal Conic Alternative',
    'leac': 'Lambert Equal Area Conic',
    'lee_os': 'Lee Oblated Stereographic',
    'loxim': 'Loximuthal',
    'lsat': 'Space oblique for LANDSAT',
    'mbt_s': 'McBryde-Thomas Flat-Polar Sine (No. 1)',
    'mbt_fps': 'McBryde-Thomas Flat-Pole Sine (No. 2)',
    'mbtfpp': 'McBride-Thomas Flat-Polar Parabolic',
    'mbtfpq': 'McBryde-Thomas Flat-Polar Quartic',
    'mbtfps': 'McBryde-Thomas Flat-Polar Sinusoidal',
    'merc': 'Mercator',
    'mil_os': 'Miller Oblated Stereographic',
    'mill': 'Miller Cylindrical',
    'misrsom': 'Space oblique for MISR',
    'moll': 'Mollweide',
    'molobadekas': 'Molodensky-Badekas transformation',
    'molodensky': 'Molodensky transform',
    'murd1': 'Murdoch I',
    'murd2': 'Murdoch II',
    'murd3': 'Murdoch III',
    'natearth': 'Natural Earth',
    'natearth2': 'Natural Earth 2',
    'nell': 'Nell',
    'nell_h': 'Nell-Hammer',
    'nicol': 'Nicolosi Globular',
    'nsper': 'Near-sided perspective',
    'nzmg': 'New Zealand Map Grid',
    'noop': 'No operation',
    'ob_tran': 'General Oblique Transformation',
    'ocea': 'Oblique Cylindrical Equal Area',
    'oea': 'Oblated Equal Area',
    'omerc': 'Oblique Mercator',
    'ortel': 'Ortelius Oval',
    'ortho': 'Orthographic',
    'pconic': 'Perspective Conic',
    'patterson': 'Patterson Cylindrical',
    'pipeline': 'Transformation pipeline manager',
    'poly': 'Polyconic (American)',
    'pop': 'Retrieve coordinate value from pipeline stack',
    'push': 'Save coordinate value on pipeline stack',
    'putp1': 'Putnins P1',
    'putp2': 'Putnins P2',
    'putp3': 'Putnins P3',
    'putp3p': "Putnins P3'",
    'putp4p': "Putnins P4'",
    'putp5': 'Putnins P5',
    'putp5p': "Putnins P5'",
    'putp6': 'Putnins P6',
    'putp6p': "Putnins P6'",
    'qua_aut': 'Quartic Authalic',
    'qsc': 'Quadrilateralized Spherical Cube',
    'robin': 'Robinson',
    'rouss': 'Roussilhe Stereographic',
    'rpoly': 'Rectangular Polyconic',
    'sch': 'Spherical Cross-track Height',
    'sinu': 'Sinusoidal (Sanson-Flamsteed)',
    'somerc': 'Swiss. Obl. Mercator',
    'stere': 'Stereographic',
    'sterea': 'Oblique Stereographic Alternative',
    'gstmerc': 'Gauss-Schreiber Transverse Mercator (aka Gauss-Laborde Reunion)',
    'tcc': 'Transverse Central Cylindrical',
    'tcea': 'Transverse Cylindrical Equal Area',
    'times': 'Times',
    'tissot': 'Tissot',
    'tmerc': 'Transverse Mercator',
    'tobmerc': 'Tobler-Mercator',
    'tpeqd': 'Two Point Equidistant',
    'tpers': 'Tilted perspective',
    'unitconvert': 'Unit conversion',
    'ups': 'Universal Polar Stereographic',
    'urm5': 'Urmaev V',
    'urmfps': 'Urmaev Flat-Polar Sinusoidal',
    'utm': 'Universal Transverse Mercator (UTM)',
    'vandg': 'van der Grinten (I)',
    'vandg2': 'van der Grinten II',
    'vandg3': 'van der Grinten III',
    'vandg4': 'van der Grinten IV',
    'vitk1': 'Vitkovsky I',
    'vgridshift': 'Vertical grid shift',
    'wag1': 'Wagner I (Kavraisky VI)',
    'wag2': 'Wagner II',
    'wag3': 'Wagner III',
    'wag4': 'Wagner IV',
    'wag5': 'Wagner V',
    'wag6': 'Wagner VI',
    'wag7': 'Wagner VII',
    'webmerc': 'Web Mercator / Pseudo Mercator',
    'weren': 'Werenskiold I',
    'wink1': 'Winkel I',
    'wink2': 'Winkel II',
    'wintri': 'Winkel Tripel'
}

 

Posted by Uli Köhler in Python

Python threading.Thread minimal example

This example shows how to subclass and start a threading.Thread:

#!/usr/bin/env python3
import threading

class MyThread(threading.Thread):
    def run(self):
        print("My thread!")

my_thread = MyThread()
my_thread.start()
print("Main thread")

This will print

My thread!
Main thread

(it might also print Main thread first, depending on what gets executed first)

Posted by Uli Köhler in Python

How to get directory of current Python script

import os 
script_directory = os.path.dirname(os.path.realpath(__file__))

This will always get the directory of the .py file running that code, so if you have .py files in a subdirectory/module this might return that subdirectory.

Original (partial) source for the solution: StackOverflow

Posted by Uli Köhler in Python

How to fix undeclared O_RDONLY/O_RDWR/O_WRONLY in C

Problem:

You are trying to compile C sourcecode that uses open(), but you see a compiler error message like this:

main.c: In function ‘main’:
main.c:3:14: warning: implicit declaration of function ‘open’ [-Wimplicit-function-declaration]
     int fd = open("my.txt", O_RDONLY);
              ^~~~
main.c:3:29: error: ‘O_RDONLY’ undeclared (first use in this function)
     int fd = open("my.txt", O_RDONLY);
                             ^~~~~~~~
main.c:3:29: note: each undeclared identifier is reported only once for each function it appears in

or

main.c: In function ‘main’:
main.c:3:14: warning: implicit declaration of function ‘open’ [-Wimplicit-function-declaration]
     int fd = open("my.txt", O_RDWR);
              ^~~~
main.c:3:29: error: ‘O_RDWR’ undeclared (first use in this function)
     int fd = open("my.txt", O_RDWR);
                             ^~~~~~
main.c:3:29: note: each undeclared identifier is reported only once for each function it appears in

or

main.c: In function ‘main’:
main.c:3:14: warning: implicit declaration of function ‘open’ [-Wimplicit-function-declaration]
     int fd = open("my.txt", O_WRONLY);
              ^~~~
main.c:3:29: error: ‘O_WRONLY’ undeclared (first use in this function)
     int fd = open("my.txt", O_WRONLY);
                             ^~~~~~~~
main.c:3:29: note: each undeclared identifier is reported only once for each function it appears in

Solution:

Add

#include <fcntl.h>

to the top of the source file where the error occured.

The POSIX header fcntl.h includes the definitions for O_RDONLY, O_RDWR and O_WRONLY amongst other POSIX-related definitions.

Also, this will include the definition for open() itself, which is missing as well – this is indicated by this line in the error message:

main.c:3:14: warning: implicit declaration of function ‘open’ [-Wimplicit-function-declaration]

See the OpenGroup reference on fcntl.h for more details on what else is defined in fcntl.h and what the meaning of all the individual flags is.

Posted by Uli Köhler in C/C++, GCC errors

Minimal pthread mutex example

#include <pthread.h>
int main() {
    // Create mutex
    pthread_mutex_t mutex;
    pthread_mutex_init(&mutex, NULL /* default mutex attributes */);
    // Lock
    pthread_mutex_lock(&mutex);
    // Unlock
    pthread_mutex_unlock(&mutex);
    // Cleanup
    pthread_mutex_destroy(&mutex);
}

Compile like this:

gcc -o main main.c -pthread
Posted by Uli Köhler in C/C++

How to fix C warning ‘implicit declaration of function _exit’

Problem:

You have C code like

_exit(1);

but when you try to compile it you see a warning message like

main.c: In function ‘main’:
main.c:3:5: warning: implicit declaration of function ‘_exit’ [-Wimplicit-function-declaration]
     _exit(1);
     ^~~~~
main.c:3:5: warning: incompatible implicit declaration of built-in function ‘_exit’

Solution:

Add

#include <unistd.h>

at the top of the source file where the error occured.

Note that in case you use _Exit(...) (capital E) instead of _exit(...), you need to add

#include <stdlib.h>

instead.

Read the manpage for _exit in case you need further information on _exit(...).

Posted by Uli Köhler in C/C++, GCC errors

How to fix C error ‘RTLD_NEXT undeclared’

Problem:

You have C code like

dlsym(RTLD_NEXT, 'myfunc');

but when you try to compile it you see an error message like

main.c:3:11: error: ‘RTLD_NEXT’ undeclared (first use in this function)
     dlsym(RTLD_NEXT, 'myfunc');
           ^~~~~~~~~

Solution:

Add

#define _GNU_SOURCE
#include <dlfcn.h>

at the top of the source file where the error occured.

In order for RTLD_NEXT to be declared, #define _GNU_SOURCE must occur before the first #include statement!

Posted by Uli Köhler in C/C++, GCC errors

How to fix C warning ‘implicit declaration of function dlsym’

Problem:

You have C code like

dlsym(RTLD_NEXT, 'myfunc');

but when you try to compile it you see a warning message like

main.c: In function ‘main’:
main.c:3:5: warning: implicit declaration of function ‘dlsym’ [-Wimplicit-function-declaration]
     dlsym(RTLD_NEXT, 'myfunc');

Solution:

Add

#include <dlfcn.h>

at the top of the source file where the error occured. This will include both dlopen, dlsym and related definitions like RTLD_NEXT.

Posted by Uli Köhler in C/C++, GCC errors

How to fix C error ‘unknown type name intptr_t’

Problem:

You have C code like

int v = 123;
intptr_t vptr = (intptr_t)&v;

but when you try to compile it you see an error message like

main.c: In function ‘main’:
main.c:5:1: error: unknown type name ‘intptr_t’; did you mean ‘__int128_t’?
 intptr_t vptr = (intptr_t)&v;
 ^~~~~~~~
 __int128_t
main.c:5:18: error: ‘intptr_t’ undeclared (first use in this function)
 intptr_t vptr = (intptr_t)&v;
                  ^~~~~~~~
main.c:5:18: note: each undeclared identifier is reported only once for each function it appears in

Solution:

Add

#include <stdint.h>

at the top of the source file where the error occured.

Posted by Uli Köhler in C/C++, GCC errors

How to fix va_list/va_arg related C error ‘expected expression before int’

Problem:

You have C code like

va_list ap;
int mode = va_arg(ap, int);

but when you try to compile it you see an error message like

main.c:5:23: error: expected expression before ‘int’
  int mode= va_arg(ap, int);
                       ^~~

Solution:

This error occurs because va_arg is undeclared. Add

#include <stdarg.h>

at the top of the source file where the error occured. This will include the definitions for va_list and the va_arg function.

Posted by Uli Köhler in C/C++, GCC errors

How to fix C error ‘unknown type name va_list’

Problem:

You have C code like

va_list ap;
int mode = va_arg(ap, int);

but when you try to compile it you see a warning message like

main.c: In function ‘main’:
main.c:4:2: error: unknown type name ‘va_list’
  va_list ap;
  ^~~~~~~

Solution:

Add

#include <stdarg.h>

at the top of the source file where the error occured. This will include the definitions for va_list and the va_arg function.

Posted by Uli Köhler in C/C++, GCC errors

How to fix C warning ‘implicit declaration of function va_arg’

Problem:

You have C code like

va_list ap;
int mode = va_arg(ap, int);

but when you try to compile it you see a warning message like

main.c:5:12: warning: implicit declaration of function ‘va_arg’ [-Wimplicit-function-declaration]
  int mode= va_arg(ap, int);

Solution:

Add

#include <stdarg.h>

at the top of the source file where the error occured. This will include the definitions for both va_list and va_arg.

Posted by Uli Köhler in C/C++, GCC errors

How to fix C error ‘mode_t undeclared’

Problem:

You have C code like

mode_t mode;

but when you try to compile it you see an error message like

main.c: In function ‘main’:
main.c:4:2: error: unknown type name ‘mode_t’
  mode_t mode;
  ^~~~~~

Solution:

Add

#include <sys/stat.h>

at the top of the source file where the error occured. In case you want further information about what mode_t represents, I recommend reading this blogpost.

Posted by Uli Köhler in C/C++, GCC errors