Reading a file or any output stream usually comes back with leading to trailing whitespace. The following code snippet removes any whitespace on the left of a c string.

char * str_ltrim(char * _arg)
{
   if (_arg == NULL)return NULL;
   int i = 0, size = 0, j = 0;
   char * result;
   size = strlens(_arg)+1;
   while (isspace(*(_arg + (i++))));
   if ((result = (char *)malloc((1 + (size - (i--)))*sizeof(char))) == NULL) return NULL;
   while (i != size)*(result + (j++)) = *(_arg + (i++));
   *(result + j) = '';
   return result;
}

You can find the complete source code here : https://gitlab.com/io.d-libraries/extend-c

Author

Write A Comment