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?
...
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...
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...
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?
...
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...
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...
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)
...
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.
...
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...
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 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...
I want to read some ruby code. And I think this is the good place to start dig in. But I cant find it.
...
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...
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...
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...
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() 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...
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 ...
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?
...
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...