stdlib

Function like SetFileLength() in C Library for Linux

Is there a function in the C Library under Linux which can set the length of a file? Under Windows I know there is a SetFileLength() function. If there is not, what is the best way of shortening a file without deleting and rewriting it? ...

Why can't gcc find the random() interface when -std=c99 is set?

I do "#include <stdlib.h>" at the top of the source. Example compilation: /usr/bin/colorgcc -std=c99 -fgnu89-inline -g -Wall -I/usr/include -I./ -I../ -I../../ -I../../../ -I../../../../ -O3 -o f8 f8.c In file included from f8.c:7: ctype-cmp.c: In function ‘randomized’: ctype-cmp.c:48: warning: implicit declaration of function ‘ra...

Evil code from the Python standard library

So, we have had this: http://lucumr.pocoo.org/2009/3/1/the-1000-speedup-or-the-stdlib-sucks. It demonstrates a rather bad bug that is probably costing the universe a load of cycles even as we speak. It's fixed now, which is great. So what parts of the standard library have you noticed to be evil? I would expect all the responsible peop...

Trouble reading a line using fscanf()

I'm trying to read a line using the following code: while(fscanf(f, "%[^\n\r]s", cLine) != EOF ) { /* do something with cLine */ } But somehow I get only the first line every time. Is this a bad way to read a line? What should I fix to make it work as expected? ...

Are strtol, strtod unsafe?

It seems that strtol() and strtod() effectively allow (and force) you to cast away constness in a string: #include <stdlib.h> #include <stdio.h> int main() { const char *foo = "Hello, world!"; char *bar; strtol(foo, &bar, 10); // or strtod(foo, &bar); printf("%d\n", foo == bar); // prints "1"! they're equal *bar = 'X'; // seg...

Scanning variable number of 'fields' from a text file

I'd like to scan a variables that form vectors from white space delimited text file and the stumbling block (all to often for me) is lack of elegance. Currently my scanning code requires delineating the size of the vector as the first element in the file: 7 : 1 3 6 8 -9 .123 1.1 Which bothers me because the '7' could be determined by...

Error in qsort function in Programming Pearls?

Hello, is it just me or this code in Programming Pearls is wrong (quicksort wants 2 const voids, no?) If so, is my solution right? Apologies, just learning... int wordncmp(char *p, char* q) { int n = k; for ( ; *p == *q; p++, q++) if (*p == 0 && --n == 0) return 0; return *p - *q; } int sortcmp(char **p, char **q) ...

Is there a strtol equivalent that does not require a null-terminated string?

Hi, Is there a standard C function similar to strtol which will take a char* and a length for a non-null-terminated string? I know that I could copy out the string into a null-terminated region, but for efficiency reasons that is undesirable. Thanks. ...

Is os.popen really deprecated in Python 2.6?

The on-line documentation states that os.popen is now deprecated. All other deprecated functions duly raise a DeprecationWarning. For instance: >>> import os >>> [c.close() for c in os.popen2('ps h -eo pid:1,command')] __main__:1: DeprecationWarning: os.popen2 is deprecated. Use the subprocess module. [None, None] The function os.pop...

Group sorting a vector in C++

I have a std::vector full of objects, each with a numeric group identifier associated with them. The object also has properties such as "size" and "name". I need to be able to sort the vector of objects by name, size and other properties while keeping them grouped together (e.g. by the group identifier mentioned above). How can this go...

I'm getting compile errors in the standard library. What's up?

I'm trying to compile an unspecified piece of software, and I'm getting errors in the standard headers such as stdio.h. The errors are mostly undeclared identifiers such as _In_. IntelliSense finds their definitions just fine. In what general direction should I look for the cause of this? Added: For example, in one cpp file stdio.h is t...

Where is the ruby standard library directory placed on a Mac?

I want to read some ruby code. And I think this is the good place to start dig in. But I cant find it. ...

Ambiguous overload call to abs(double)

I have the following C++ code: #include <math.h> #include <cmath.h> // per http://www.cplusplus.com/reference/clibrary/cmath/abs/ // snip ... if ( (loan_balance < 0) && (abs(loan_balance) > loan_payment) ) { ... } and make blows up on: error: call of overloaded 'abs(double)' is ambiguous also of interest: /usr/include/s...

Infinite loop on EOF in C++

This code works as desired for the most part, which is to prompt the user for a single character, perform the associated action, prompt the user to press return, and repeat. However, when I enter ^D (EOF) at the prompt, an infinite loop occurs. I am clearing the error state via std::cin.clear() and calling std::cin.ignore(...) to clear...

Why does stdlib.h's abs() family of functions return a signed value?

The negative implication of this is noted in the man page: NOTES Trying to take the absolute value of the most negative integer is not defined. What's the reasoning behind this and what's the best recourse for a person who would like to avoid undefined behavior? Do I have to resort to something like: unsigned...

Stdlib itoa function

Hi, I searched on internet and saw a lot of code that uses itoa() function & they claimed that this function is in stdlib.h I'm using 2 versions of GCC right now: (GCC) 4.2.4 (Ubuntu 4.2.4-1ubuntu4)) (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44) and both of them does not have itoa() function (I compile the program & error: undefined ref...

std::map find doesn't work properly

std::map.find() is intended to return an map::iterator to an element it found if there is some or to the end() if not. I get BadPtr returned to me. The exactly same construct works fine in the other part of code. What is this? class OntologyContainer { map<string, OntologyClass*> data; OntologyClass* last_added; public: clas...

C++ code runs with missing header, why?

I just realized that I am supposed to include the #include<cstdlib> required by abs() for the abs() function. #include<iostream> using namespace std; int main() { int result; result = abs(-10); cout << result << "\n"; return 0; } Why does this code still work, even though I forgot the ...

Where is os.environ initialized?

Using this code, many keys are output, but I expected no output: import os for i in os.environ: print i This is the code from os.py: try: environ except NameError: environ = {} Where does os.environ get its values from? Where is it initialized? ...

fgets() function in C

Hi, I know everybody has told me to use fgets and not gets because of buffer overflow. However, I am a bit confused about the third parameter in fgets(). As I get it, fgets is dependent on: char * fgets ( char * str, int num, FILE * stream ); char* str is the ptr to where my input will be stored. num is the max number of characte...