Hey guys i am trying to read a string using fgets and storing in an array i want to prevent fgets from storing the new line character on the array using the shortest means possible..
Hey guys i am trying to read a string using fgets and storing in an array i want to prevent fgets from storing the new line character on the array using the shortest means possible..
This is a one version.
You need to include string.h to use strlen().Code:int len=strlen(buff); //where buff is your char array fgets is using if(buff[len-1]=='\n') buff[len-1]='\0';
The only way to "prevent" fgets() from storing the newline is to enter exactly as many characters as expected, indicated in the functional call (second argument minus one). This is certainly not an ideal solution.
Otherwise, you must find and remove the newline yourself. There was quite a discussion on this concept in this thread.