<< < 9 10 11 12 13 14 15 16 17 18 19 > >>

How can I invoke another program or command and trap its output?
How can I invoke another program or command and trap its output? Unix and some other systems provide a popen function, which sets up a stdio stream on a pipe connected to the process running a command, so that the calling program can read the output (or alternatively supply the input). Using popen, ...
2015-03-16, 1167👍, 0💬

How can my program discover the complete pathname to the executable from which it was invoked?
How can my program discover the complete pathname to the executable from which it was invoked? argv[0] may contain all or part of the pathname, or it may contain nothing. You may be able to duplicate the command language interpreter's search path logic to locate the executable if the name in argv[0]...
2015-03-13, 1137👍, 0💬

How can I automatically locate a programs configuration files in the same directory as the executable?
How can I automatically locate a programs configuration files in the same directory as the executable? It's hard, in general; Even if you can figure out a workable way to do it, you might want to consider making the program's auxiliary (library) directory configurable, perhaps with an environment va...
2015-03-13, 1158👍, 0💬

How can a process change an environment variable in its caller?
How can a process change an environment variable in its caller? It may or may not be possible to do so at all. Different operating systems implement global name/value functionality similar to the Unix environment in different ways. Whether the ``environment'' can be usefully altered by a running pro...
2015-03-11, 1147👍, 0💬

How can I open files mentioned on the command line, and parse option flags?
How can I open files mentioned on the command line, and parse option flags? Here is a skeleton which implements a traditional Unix-style argv parse, handling option flags beginning with -, and optional filenames. (The two flags accepted by this example are -a and -b; -b takes an argument.) #include ...
2015-03-11, 1230👍, 0💬

Is exit(status) truly equivalent to returning the same status from main?
Is exit(status) truly equivalent to returning the same status from main? Yes and no. The Standard says that a return from the initial call to main is equivalent to calling exit. However, a return from main cannot be expected to work if data local to main might be needed during cleanup; A few very ol...
2015-03-09, 1178👍, 0💬

How can I read in an object file and jump to locations in it?
How can I read in an object file and jump to locations in it? You want a dynamic linker or loader. It may be possible to malloc some space and read in object files, but you have to know an awful lot about object file formats, relocation, etc., and this approach can't work if code and data reside in ...
2015-03-09, 1187👍, 0💬

How can I implement a delay, or time a users response, with sub-second resolution?
How can I implement a delay, or time a users response, with sub-second resolution? Unfortunately, there is no portable way. Routines you might look for on your system include clock, delay, ftime, gettimeofday, msleep, nap, napms, nanosleep, setitimer, sleep, Sleep, times, and usleep. (A function cal...
2015-03-06, 1125👍, 0💬

How can I trap or ignore keyboard interrupts like control-C?
How can I trap or ignore keyboard interrupts like control-C? The basic step is to call signal, either as #include &lt;signal.h> signal(SIGINT, SIG_IGN); to ignore the interrupt signal, or as extern void func(int); signal(SIGINT, func); to cause control to transfer to function func on receipt of ...
2015-03-06, 1139👍, 0💬

How can I handle floating-point exceptions gracefully?
How can I handle floating-point exceptions gracefully? On many systems, you can define a function matherr which will be called when there are certain floating-point errors, such as errors in the math routines in &lt;math.h>. You may also be able to use signal to catch SIGFPE
2015-03-04, 1164👍, 0💬

How can I ensure that integer arithmetic doesnt overflow?
How can I ensure that integer arithmetic doesnt overflow? The usual approach is to test the operands against the limits in the header file &lt;limits.h> before doing the operation. For example, here is a ``careful'' addition function: int chkadd(int a, int b) { if(INT_MAX - b &lt; a) { fputs...
2015-03-04, 1168👍, 0💬

I am trying to compile this program
I'm trying to compile this program, but the compiler is complaining that ``union REGS'' is undefined, and the linker is complaining that int86 is undefined. Those have to do with MS-DOS interrupt programming. They don't exist on other systems.
2015-03-02, 1143👍, 0💬

What are near and far pointers?
What are near and far pointers? These days, they're pretty much obsolete; they're definitely system-specific. They had to do with 16-bit programming under MS-DOS and perhaps some early versions of Windows. If you really need to know, see a DOS- or Windows-specific programming reference. If you're us...
2015-03-02, 1132👍, 0💬

But I cant use all these nonstandard, system-dependent functions, because my program has to be ANSI compatible!
But I cant use all these nonstandard, system-dependent functions, because my program has to be ANSI compatible! You're out of luck. Either you misunderstood your requirement, or it's an impossible one to meet. ANSI/ISO Standard C simply does not define ways of doing these things; it is a language st...
2015-02-27, 1172👍, 0💬

Why isnt any of this standardized in C? Any real program has to do some of these things.
Why isnt any of this standardized in C? Any real program has to do some of these things. Actually, some standardization has occurred along the way. In the beginning, C did not have a standard library at all; programmers always had to ``roll their own'' utility routines. After several abortive attemp...
2015-02-27, 1188👍, 0💬

How can I return multiple values from a function?
How can I return multiple values from a function? There are several ways of doing this. (These examples show hypothetical polar-to-rectangular coordinate conversion functions, which must return both an x and a y coordinate.) 1. Pass pointers to several locations which the function can fill in: #incl...
2015-02-25, 1141👍, 0💬

What is a good data structure to use for storing lines of text?
What's a good data structure to use for storing lines of text? I started to use fixed-size arrays of arrays of char, but they're just too restrictive. One good way of doing this is with a pointer (simulating an array) to a set of pointers (each simulating an array) of char. This data structure is so...
2015-02-25, 1227👍, 0💬

What is the right way to use errno?
What is the right way to use errno? In general, you should detect errors by checking return values, and use errno only to distinguish among the various causes of an error, such as ``File not found'' or ``Permission denied''. (Typically, you use perror or strerror to print these discriminating error ...
2015-02-23, 1159👍, 0💬

How can I write data files which can be read on other machines with different word size, byte order, or floating point formats?
How can I write data files which can be read on other machines with different word size, byte order, or floating point formats? The most portable solution is to use text files (usually ASCII), written with fprintf and read with fscanf or the like. (Similar advice also applies to network protocols.) ...
2015-02-23, 1185👍, 0💬

If I have a char * variable pointing to the name of a function ...
If I have a char * variable pointing to the name of a function, how can I call that function? Code like extern int func(int, int); char *funcname = "func"; int r = (*funcname)(1, 2); or r = (*(int (*)(int, int))funcname)(1, 2); doesn't seem to work. By the time a program is running, information abou...
2015-02-20, 1262👍, 0💬

How can I manipulate individual bits??
How can I manipulate individual bits?? Bit manipulation is straightforward in C, and commonly done. To extract (test) a bit, use the bitwise AND (&amp;) operator, along with a bit mask representing the bit(s) you're interested in: value &amp; 0x04 To set a bit, use the bitwise OR (| or |=) o...
2015-02-20, 1246👍, 0💬

How can I determine whether a machines byte order is big-endian or little-endian?
How can I determine whether a machines byte order is big-endian or little-endian? The usual techniques are to use a pointer: int x = 1; if(*(char *)&amp;x == 1) printf("little-endian\n"); else printf("big-endian\n"); or a union: union { int i; char c[sizeof(int)]; } x; x.i = 1; if(x.c[0] == 1) p...
2015-02-16, 1327👍, 0💬

How do I swap bytes?
How do I swap bytes? V7 Unix had a swab function, but it seems to have been forgotten. A problem with explicit byte-swapping code is that you have to decide whether to call it or not, based on the byte order of the data and the byte order of the machine in use. A better solution is to define functio...
2015-02-16, 1392👍, 0💬

How can I convert integers to binary or hexadecimal?
How can I convert integers to binary or hexadecimal? Make sure you really know what you're asking. Integers are stored internally in binary, although for most purposes it is not incorrect to think of them as being in octal, decimal, or hexadecimal, whichever is convenient. The base in which a number...
2015-02-13, 1155👍, 0💬

<< < 9 10 11 12 13 14 15 16 17 18 19 > >>