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.
strcpy(my_char, my_char+1);
strcpy
strings must not overlap.
From the C Standard (emphasis mine):
(C99, 7.21.2.3p2) "The strcpy function copies the string pointed to by s2 (including the terminating null character) into the array pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With