c

Number of bytes read from a pipe

When reading from a pipe in Linux (C, fread/similar), when EOF is reached, how can it be known how many bytes were read? If I read blocks at a time, fread() only returns the number of full blocks read in, and I can't read one byte at a time because that is too slow. Of course, ftell() returns -1. ...

Including */ in a C-style block comment

Is there any way to include */ in a C-style block comment? Changing the block comment to a series of line comments (//) is not an option in this case. Here's an example of the sort of comment causing a problem: /** * perl -pe 's/(?<=.{6}).*//g' : Limit to PID */ ...

Reading from file using fgets() causes "Access Violation reading from address..." c++

I'm Using FILE * F opened in _sfopen () I'm reading from the file in while(!feof(f)) and then fgets(str,1024,f) when running reaches the last line it still goes in the while but then when trying to fgets it flies out with an access violation arror (menwhile I just put a try and catch(...) but I know It's not a good solution ) what shou...

Using typedefs (or #defines) on built in types - any sensible reason?

Well I'm doing some Java - C integration, and throught C library werid type mappings are used (theres more of them;)): #define CHAR char /* 8 bit signed int */ #define SHORT short /* 16 bit signed int */ #define INT int /* "natural" len...

building a web crawler

im currently developing a custom search engine with built-in web crawler. for some reason im not into multi-threading, thus so far my indexer was coded in single-threaded manner. Now i have a small dilema with crawler im building, can anybody suggest which is better, crawl 1 page then index it, or crawl 1000+ page and cache then index? ...

What issues can I expect compiling C code with a C++ compiler?

If you take an existing C code base and compile it with a C++ compiler, what sort of issues can you expect to crop up? For example, I think that assigning an integer to an value with an enumerated type will fail in C++, whereas it's legal ( if a bit nasty ) in C. If I don't wrap all my C files in "extern C { ... }", am I going to get na...

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? ...

why do I get errno EINVAL (innvalid parameters) but only when the file is empty

I use _fsopen(path, "r+", _SH_DENYRW) for opening a file in C any parameter for protection (_SH_...) cause the same issue. When opening an empty file, errno is set to 22 (EINVAL), not so when the file isn't empty - then all is OK. What can I do? ...

Count the network interfaces with WSAIoctl function (WIN32 API)

I'm trying to list available interfaces using the WSAIoctl function. I have to pass in a buffer to hold the complete list. I want to get a count of the interfaces before I allocate memory to hold the interface details but if I pass in a NULL pointer the call just fails (I dont get a valid count returned). Any way to get this count befor ...

How can I break out of two nested for loops in objective-c?

I have two for loops nested like this: for(...) { for(...) { } } I know that there is a break; statement. But I am confused about if it breaks both loops or just the one in which it was called? I need to break both ones as soon as I see that it doesn't make sense to iterate more times over. ...

Does the OS (POSIX) flush a memory-mapped file if the process is SIGKILLed?

If a process is killed with SIGKILL, will the changes it has made to a memory-mapped file be flushed to disk? I assume that if the OS ensures a memory-mapped file is flushed to disk when the process is killed via SIGKILL, then it will also do so with other terminating signals (SIGABRT, SIGSEGV, etc...). ...

Using nibbles (4 bits variables) in windows C/C++

I'm programming network headers and a lot of protocols use 4 bits fields. Is there a convenient type I can use to represent this information? The smallest type I've found is a BYTE. I must then use a lot of binary operations to reference only a few bits inside that variable. ...

Registering Windows Classes Win32

Hi, what's the best practise for registering window classes for a win32 program? Do you register every class in the WinMain function or as they're required? ...

Passing structures as arguments while using pthread_create()

I tried passing a structure as the 4th argument while using pthread_create() with something like this: pthread_create(&tid1, NULL, calca, &t); //t is the struct Now whenever I try to access variables in the structure - t.a, t.b or t.c, I keep getting an error - request for member in something not a structure or union. What alternate ...

Why does an 8-bit field have endianness?

See the definition of TCP header in /netinet/tcp.h: struct tcphdr { u_int16_t th_sport; /* source port */ u_int16_t th_dport; /* destination port */ tcp_seq th_seq; /* sequence number */ tcp_seq th_ack; /* acknowledgement number */ # if __BYTE_ORDER == __LITTLE_ENDIAN u_int8...

What happens to memory that is not freed after end of program?

Duplicate: What REALLY happens when you don’t free after malloc? Let's say, for example: int main() { char* test = new char[50000]; return 0; } What happens to the allocated memory after the program had finished? Does it get freed for other applications immediately? Or perhaps after some time? Or maybe it's lost to the system...

Elegant way of holding large static typesafe dictionary in java - or avoiding code too large

Basically I would like to have some dictionary that is an abstaction over legacy #define directives. I have an old header file that contains 6000+ defines, that are used as flag parametersome function and these defines denote one type of entity parameter. In C i have GetParameter(... , T_CTITLE, ...); In Java i would like to call...

What is the easiest way to get an int in a console app?

I want to process user input as an integer, but it seems as though C has no way to get an int from stdin. Is there a function to do this? How would I go about getting an int from the user? ...

When/why is it a bad idea to use the fscanf() function?

In an answer there was an interesting statement: "It's almost always a bad idea to use the fscanf() function as it can leave your file pointer in an unknown location on failure. I prefer to use fgets() to get each line in and then sscanf() that." Could you expand upon when/why it might be better to use fgets() and sscanf() to read some ...

Understanding Objective c enum declaration

From iPhone UIControl UIControlEventAllTouchEvents = 0x00000FFF, UIControlEventAllEditingEvents = 0x000F0000, UIControlEventApplicationReserved = 0x0F000000, UIControlEventSystemReserved = 0xF0000000, UIControlEventAllEvents = 0xFFFFFFFF Now I assume the UIControlEventApplication is the 'range' I can use to spe...