经常使用字符串处理函数

strcat - 字符串拼接函数

char *strcat (char targetString[], const char string_toAppend[]);数组

Purpose

Appends [ə'pend附加] a copy of a source string to the end of a target string, including the terminating ASCII NUL byte. The initial [ɪ'nɪʃəl最初的]character of the string to append[ə'pend附加] overwrites the NUL[空字符] byte[] at the end of the target string. If copying takes place between objects that overlap, the behavior is undefined.app

把sourcestring所指字符串添加到targetString结尾处 ( 覆盖dest结尾处的'\0' )并添加'\0'。sourcestring和targetString所指内存区域不能够重叠且targetString必须有足够的空间来容纳sourcestring的字符串。

sprintf -- 字符串格式化命令

int sprintf (char targetString[], const char formatString[], ...);函数

Purpose

Writes output to the specified string according to format specifiers in formatString. A null character is written at the end of the characters written.spa

主要功能是把格式化的数据写入某个字符串中。 code

#include<stdio.h>

//某个头文件 intmain()/*主函数“整数”类型*/
{
  charbuffer[50];                    /* “字符”类型的数组,下面共有50个元素。*/
  int n,a=5,b=3;                     /* 三个变量都为“整数”类型*/
  n=sprintf(buffer,"%dplus%dis%d",a,b,a+b);   /* 赋予数值*/
  printf("[%s]isastring%dcharslong\n",buffer,n);  /* “格式输出”*/
  return0;                        /* “返回零” 也就是程序正常退出*/
}
输出结果:
[5 plus 3 is 8]     is a string 13 chars long

strcpy -- 字符串拷贝函数//
----------------------------------------------------------------------------------------------得到文件路径 int GetIniFilePath(char*filename,char*filepath,char *errmsg) { char CurrentFilePath [1024] ={"\0"}; char FilePathConfFile [1024]={"\0"}; if(GetDir (CurrentFilePath)<0) { return -1; } strcat(CurrentFilePath,"\\Config"); sprintf (FilePathConfFile, "%s\\%s.ini", CurrentFilePath,filename); strcpy(filepath,FilePathConfFile); return 0; }

strcpy--字符串拷贝函数

char *strcpy (char targetString[], const char sourceString[]);orm

Purpose

Copies a source string including the terminating ASCII NUL byte into a target string. If copying takes place between objects that overlap[əʊvə'læp重叠], the behavior is undefined. 【src和dest所指内存区域不能够重叠且dest必须有足够的空间来容纳src的字符串】blog

相关文章
相关标签/搜索