Difference between scanf() and fgets()

There are multiple differences. Two crucial ones are: fgets() can read from any open file, but scanf() only reads standard input. fgets() reads ‘a line of text’ from a file; scanf() can be used for that but also handles conversions from string to built in numeric types. Many people will use fgets() to read a … Read more

C – scanf() vs gets() vs fgets()

Never use gets. It offers no protections against a buffer overflow vulnerability (that is, you cannot tell it how big the buffer you pass to it is, so it cannot prevent a user from entering a line larger than the buffer and clobbering memory). Avoid using scanf. If not used carefully, it can have the … Read more

How to read from stdin with fgets()?

here a concatenation solution: #include <stdio.h> #include <stdlib.h> #include <string.h> #define BUFFERSIZE 10 int main() { char *text = calloc(1,1), buffer[BUFFERSIZE]; printf(“Enter a message: \n”); while( fgets(buffer, BUFFERSIZE , stdin) ) /* break with ^D or ^Z */ { text = realloc( text, strlen(text)+1+strlen(buffer) ); if( !text ) … /* error handling */ strcat( text, … Read more

Removing trailing newline character from fgets() input

Perhaps the simplest solution uses one of my favorite little-known functions, strcspn(): buffer[strcspn(buffer, “\n”)] = 0; If you want it to also handle ‘\r’ (say, if the stream is binary): buffer[strcspn(buffer, “\r\n”)] = 0; // works for LF, CR, CRLF, LFCR, … The function counts the number of characters until it hits a ‘\r’ or … Read more

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