C read file line by line

I wrote this function to read a line from a file: The function reads the file correctly, and using printf I see that the constLine string did get read correctly as well. However, if I use the function e.g. like this: printf outputs gibberish. Why?

hat does “static” mean in C?

A static variable inside a function keeps its value between invocations. A static global variable or a function is “seen” only in the file it’s declared in (1) is the more foreign topic if you’re a newbie, so here’s an example: This prints: This is useful for cases where a function needs to keep some … Read more

getopt_long() — proper way to use it?

First off, you probably don’t want 0 for the has_arg field – it must be one of no_argument, required_arguemnt, or optional_argument. In your case, all of them are going to be required_argument. Besides that, you’re not using the flag field correctly – it has to be an integer pointer. If the corresponding flag is set, getopt_long() will fill it in with the integer you passed in via … Read more

Categories C Tags

Understanding INADDR_ANY for socket programming

bind() of INADDR_ANY does NOT “generate a random IP”. It binds the socket to all available interfaces. For a server, you typically want to bind to all interfaces – not just “localhost”. If you wish to bind your socket to localhost only, the syntax would be my_sockaddress.sin_addr.s_addr = inet_addr(“127.0.0.1”);, then call bind(my_socket, (SOCKADDR *) &my_sockaddr, …). As it happens, INADDR_ANY is a constant that happens … Read more

Get text from user input using C

You use the wrong format specifier %d– you should use %s. Better still use fgets – scanf is not buffer safe. Go through the documentations it should not be that difficult: scanf and fgets Sample code: Input: Output:

Categories C Tags

What is activation record in the context of C and C++?

An activation record is another name for Stack Frame. It’s the data structure that composes a call stack. It is generally composed of: Locals to the callee Return address to the caller Parameters of the callee The previous stack pointer (SP) value The Call Stack is thus composed of any number of activation records that … Read more

In C, how should I read a text file and print all strings

The simplest way is to read a character, and print it right after reading: c is int above, since EOF is a negative number, and a plain char may be unsigned. If you want to read the file in chunks, but without dynamic memory allocation, you can do: The second method above is essentially how you will read a file with a dynamically … Read more

sizeof float (3.0) vs (3.0f)

Because 3.0 is a double. See C syntax Floating point types. Floating-point constants may be written in decimal notation, e.g. 1.23. Scientific notation may be used by adding e or E followed by a decimal exponent, e.g. 1.23e2 (which has the value 123). Either a decimal point or an exponent is required (otherwise, the number is … Read more

Linux equivalent of I_PUSH

TIOCPKT is exactly what you want, according to the tty_ioctl(4) man page: the argument is a pointer to an integer which is non-zero to enable packet mode, or zero to disable it.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)