I am on a (ubuntu precise) linux system and I want to remove leading characters (tabulators) from a string in C. I thought the following code was working on my previous installation (ubuntu oneric) but I found now that it does not work anymore (note that this is a simplified version of a code for general UTF-8 characters):
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
int nbytes = 10000;
char *my_line, *my_char;
my_line = (char *)malloc((nbytes + 1)*sizeof(char));
strcpy(my_line,"\tinterface(quiet=true):");
printf("MY_LINE_ORIG=%s\n",my_line);
while((my_char=strchr(my_line,9))!=NULL){
strcpy(my_char, my_char+1);
}
printf("MY_LINE=%s\n",my_line);
return 0;
}
I do
gcc -o removetab removetab.c
When executing removetab I get
MY_LINE_ORIG= interface(quiet=true):
MY_LINE=interfae(quiet==true):
Note the dublication of “=” and the missing “c”!
Whats wrong or how can I achieve this alternatively. The code should support UTF-8 strings.
strcpystrings must not overlap.From the C Standard (emphasis mine):