經常使用字符串處理函數

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

相關文章
相關標籤/搜索