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?

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

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.

Expression preceding parentheses?

The error is referring to this line: Here you’re (apparently) trying to call delay as a function. However, delay is declared as: It’s just a number, not a function, hence you can’t call it.

Categories C Tags