hig*_*guy 5 c linux string std
我在(ubuntu精确)linux系统上,我想从C中的字符串中删除前导字符(制表符).我认为以下代码正在处理我以前的安装(ubuntu oneric),但我发现它现在不起作用(请注意,这是一般UTF-8字符代码的简化版本):
#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;
}
Run Code Online (Sandbox Code Playgroud)
我做
gcc -o removetab removetab.c
Run Code Online (Sandbox Code Playgroud)
执行removetab时我得到了
MY_LINE_ORIG= interface(quiet=true):
MY_LINE=interfae(quiet==true):
Run Code Online (Sandbox Code Playgroud)
注意"="和缺少"c"的出版物!什么是错的,或者我怎样才能实现这个目标.代码应该支持UTF-8字符串.
strcpy(my_char, my_char+1);
Run Code Online (Sandbox Code Playgroud)
strcpy 字符串不得重叠.
从C标准(强调我的):
(C99,7.21.2.3p2)"strcpy函数将s2指向的字符串(包括终止空字符)复制到s1指向的数组中.如果在重叠的对象之间进行复制,则行为未定义. "
| 归档时间: |
|
| 查看次数: |
837 次 |
| 最近记录: |