Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
programming:howtos-tips [2008/11/15 08:29] cyril |
programming:howtos-tips [2014/04/16 18:07] (current) cyril [Conversions with string] |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Howtos and Tips ====== | ====== Howtos and Tips ====== | ||
+ | ===== System ===== | ||
- | ===== Catching signals | + | For details on functions search in the man page ('' |
+ | |||
+ | ==== Catching signals ==== | ||
=> C and C++. | => C and C++. | ||
Line 33: | Line 36: | ||
You can also change the signals actions with the [[http:// | You can also change the signals actions with the [[http:// | ||
+ | ==== Timer ==== | ||
+ | |||
+ | <code c> | ||
+ | #include < | ||
+ | unsigned int alarm(unsigned int seconds); | ||
+ | </ | ||
+ | will trigger signal SIGALRM x seconds later that you can catch (see [[# | ||
+ | |||
+ | If you need automatic repetition and/or sub-second precision, you can use: | ||
+ | <code c> | ||
+ | #include < | ||
+ | int setitimer(int whichtimer, const struct itimerval *value, struct itimerval *ovalue); | ||
+ | </ | ||
+ | |||
+ | ==== Waiting event ==== | ||
+ | |||
+ | Monitor multiple file descriptors. | ||
+ | |||
+ | <code c> | ||
+ | #include < | ||
+ | int select(int nfds, fd_set *readfds, fd_set *writefds, | ||
+ | | ||
+ | </ | ||
+ | |||
+ | ==== Posix Threads ==== | ||
+ | |||
+ | <code c> | ||
+ | #include < | ||
+ | |||
+ | void *threadFunction(void *threadid) | ||
+ | { | ||
+ | printf(" | ||
+ | pthread_exit(NULL); | ||
+ | } | ||
+ | |||
+ | int main (int argc, char *argv[]) | ||
+ | { | ||
+ | pthread_t threadId; | ||
+ | int threadData = 42; | ||
+ | if (pthread_create(& | ||
+ | { | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | FIXME join sem etc | ||
+ | |||
+ | ==== Change program owner ==== | ||
+ | |||
+ | <code c> | ||
+ | pwd = getpwnam(" | ||
+ | if (pwd) | ||
+ | { | ||
+ | setgid(pwd-> | ||
+ | setuid(pwd-> | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | But it doesn' | ||
+ | <code c> | ||
+ | system(" | ||
+ | system(" | ||
+ | </ | ||
===== Formatting IOs (iomanip) ===== | ===== Formatting IOs (iomanip) ===== | ||
Line 44: | Line 113: | ||
for everything in this section. | for everything in this section. | ||
- | ==== Color ==== | + | === Color === |
<code cpp> | <code cpp> | ||
- | std::cout << " | + | std::cout << " |
- | << " | + | << " |
<< " | << " | ||
</ | </ | ||
Line 66: | Line 135: | ||
- | ==== Floating point precision | + | === Floating point precision === |
<code cpp> | <code cpp> | ||
Line 76: | Line 145: | ||
- | ==== Alignment | + | === Alignment === |
Line 85: | Line 154: | ||
</ | </ | ||
- | ==== White spaces | + | === White spaces === |
To get all characters while they are white spaces. | To get all characters while they are white spaces. | ||
Line 113: | Line 182: | ||
<code c> | <code c> | ||
#define ERROR(msg, ...) fprintf(" | #define ERROR(msg, ...) fprintf(" | ||
+ | </ | ||
+ | You can also use the '' | ||
+ | < | ||
+ | #define ERROR(msg, ...) fprintf(" | ||
</ | </ | ||
Line 122: | Line 195: | ||
===== Conversions with string ===== | ===== Conversions with string ===== | ||
- | ==== From * to string | + | === From * to string === |
=> C++ | => C++ | ||
Line 137: | Line 210: | ||
#include < | #include < | ||
char s[128]; | char s[128]; | ||
- | sprintf(" | + | sprintf(s, " |
</ | </ | ||
- | ==== From string to * ==== | + | === From string to * === |
=> C & C++ | => C & C++ | ||
Line 146: | Line 219: | ||
#include < | #include < | ||
int i = atoi(" | int i = atoi(" | ||
- | float f = atof("4.5"); | + | long l = atol("4500294"); |
- | double | + | double |
</ | </ | ||
- | ===== Define | + | ===== # |
You can define names for the preprocessor from the command line (or the makefile) instead of the source file (''# | You can define names for the preprocessor from the command line (or the makefile) instead of the source file (''# | ||
Line 165: | Line 238: | ||
===== Function pointers ===== | ===== Function pointers ===== | ||
+ | |||
The full test program is available [[http:// | The full test program is available [[http:// | ||
- | ==== Normal fn ==== | + | === Normal fn === |
<code cpp> | <code cpp> | ||
Line 186: | Line 260: | ||
</ | </ | ||
- | ==== Static member fn ==== | + | === Static member fn === |
Static member functions behave the same way than normal functions. | Static member functions behave the same way than normal functions. | ||
Line 197: | Line 271: | ||
</ | </ | ||
- | ==== Non virtual member fn ==== | + | === Non virtual member fn === |
Member functions have a hidden parameter (a pointer to '' | Member functions have a hidden parameter (a pointer to '' | ||
Line 208: | Line 282: | ||
</ | </ | ||
- | ==== Virtual member fn ==== | + | === Virtual member fn === |
The syntax is the same as for non virtual member functions, and the pointer points to the place in the object where the address of the function is stored, so that it always points to the right function. | The syntax is the same as for non virtual member functions, and the pointer points to the place in the object where the address of the function is stored, so that it always points to the right function. | ||
Line 223: | Line 297: | ||
===== Cast overloading ===== | ===== Cast overloading ===== | ||
- | ==== Cast from ==== | + | === Cast from === |
This is done with constructors : | This is done with constructors : | ||
Line 242: | Line 316: | ||
</ | </ | ||
- | ==== Cast to ==== | + | === Cast to === |
The syntax is simple : | The syntax is simple : | ||
Line 251: | Line 325: | ||
| | ||
... | ... | ||
- | double() { return data / (double)(1<< | + | |
}; | }; | ||
Line 259: | Line 333: | ||
</ | </ | ||
- | ==== Template cast ==== | + | === Template cast === |
Imagine the following situation : | Imagine the following situation : |