+ 2
Anyone can explain me the uses of fgetsïŒfputs,sprintf,sscanf specifically in a easy way
I think that the tutorial is over complicated and precise.
1 Answer
+ 6
fgets() - is most often used to get a fixed-length string as input from stdin (but can fetch strings from files too.)
This function also saves the '\n' character to the string (which is not always what you want).
fputs() - outputs a string to any file you choose (most often stdout).
sscanf() - looks for data in a string and assigns it to variable(s) with a matching type.
ex: nr_read = sscanf(input, "%d%d", &num1, &num2);
sprintf() - works similar to printf() it creates a formatted string but unlike printf() it stores it into a string. This is useful when you want to convert numbers to a string representation, ex: for time where you have hours, minutes and seconds.
sprintf(time_string, "%02d:%02d:%02d", hour, min, sec);
sprintf() - should be used with caution since it doesn't know the size of the string it writes to - which can lead to buffer overflows, so make sure the string is large enough to hold the data. For C99 there's snprintf() which checks the size.