C 常用函数实例
1、strcmp:字符串比較
if (strcmp(pOpt, "HASH") == 0)0:相同大于0:字符串1大于字符串2小于0:字符串1小于字符串22、memset:清空結構體
memset(&stTest,0,sizeof(struct sample_struct));如果是數組:struct sample_struct TEST[10]; 則 memset(TEST,0,sizeof(struct sample_struct)*10);3、sscanf:格式取值
sscanf( “abcdef ghijk”, “%s“, s);//遇到空格賦值截止puts( s );//abcdefsscanf( “abcdefghijk “, “%4s“, s);//取指定長度的字符串puts(s);//abcdsscanf( “abcdefghijk “+4, “%s”, s);//略去指定長度,取后面的字符串puts(s);//efghijksscanf( “abcdefghijk”, “%[^i]“, s);//取指定字符截止的字符串puts(s);//abcdefghsscanf( “abcdefghijkLMNOPQ”, “%[^A-Z]“, s);//取指定字符集截止的字符串puts(s);//abcdefghijksscanf( “abcdefg#hijkLMN@OPQ”, “%*[^#]#%[^@]“, s);//取指定字符之間的字符串puts(s);//hijkLMNsscanf( “hello world!”, “%*s%s“, s);puts(s);//world!注意:“%[^f]” , ^ 的意思是非%*d和%*s , * 是表示跳過此數據不讀入. (也就是不把此數據讀入參數中)4、strcpy:字符串拷貝(char數組)
strcpy(xmlInfo.szProtocol, m_protoName.GetBuffer());5、strncasecmp:用來比較參數s1和s2字符串前n個字符,比較時會自動忽略大小寫的差異。
相關函數:bcmp, memcmp, strcmp, strcoll, strncmp 表頭文件:#include <strings.h> 函數定義:int strncasecmp(const char *s1, const char *s2, size_t n) 函數說明:strncasecmp()用來比較參數s1和s2字符串前n個字符,比較時會自動忽略大小寫的差異。6、strncmp:這個函數用來比較s1和s2字符串的前maxlen個字符
int strncmp ( const char * str1, const char * str2, size_t num );這個函數用來比較s1和s2字符串的前maxlen個字符。如果兩個字符串相等的話,strncmp將返回0。如果s1是s2的一個子串的話,s1小于s2。此外還有,函數 int strncmp (const char *s1, const char *s2, size_t size) 此函數與strcmp極為類似。不同之處是,strncmp函數是指定比較size個字符。也就是說,如果字符串s1與s2的前size個字符相同,函數返回值為0。7、chdir:是C語言中的一個系統調用函數(同cd),用于改變當前工作目錄,其參數為Path 目標目錄,可以是絕對目錄或相對目錄。
#include <unistd.h>int chdir(const char *path);8、umask(0);
chdir("/"); umask(0);9、strcat 字符串相加
char p3[20] = "111"; char* p4 = "222"; strcat(p3, p4); printf("%s\n", p3);10、clrscr:進入文件夾后修改文件默認權限,與chmod相反
清除文本模式窗口 清屏的意思 就是把之前顯示出的文字字符去掉 跟cmd里面的清屏的功能是一樣的。11、memcpy(void *dest, void *src, unsigned int count):將s中第13個字符開始的4個連續字符復制到d中。(從0開始)
#include<string.h>int main( {char* s="GoldenGlobalView";char d[20];memcpy(d,s+12,4); //從第13個字符(V)開始復制,連續復制4個字符(View)d[4]='\0'; //memcpy(d,s+14*sizeof(char),4*sizeof(char));也可printf("%s",d);getchar();return 0; }輸出結果: View12. int sprintf( char *buffer, const char *format [, argument] … ):把格式化的數據寫入某個字符串
函數功能:把格式化的數據寫入某個字符串 函數原型:int sprintf( char *buffer, const char *format [, argument] … ); 返回值:字符串長度(strlen) 例子: char* who = "I"; char* whom = "CSDN"; sprintf(s, "%s love %s.", who, whom); //產生:"I love CSDN. " 這字符串寫到s中 sprintf(s, "%10.3f", 3.1415626); //產生:" 3.142"13. (long)getpid():獲取當前進程ID
返回值:int14. char *getlogin() : 獲取系統登陸時間用戶名
#include <unistd.h>15. exit(0)與_exit(0)的區別在于一個父進程退出,一個子進程退出
16. getpgid(),? ?int? setpgid(pid_t pid, pid_t pgid) : 獲取and設置進程組
#include <stdio.h> #include <stdlib.h> #include <unistd.h>int main() {pid_t pid;if ((pid = fork()) < 0) {perror("fork");exit(1);}else if (pid == 0) {printf("child PID = %d\n", getpid());printf("child Group ID = %d\n", getpgid(0)); //返回組idsleep(7);printf("-------Group ID of child id change to %d\n", getpgid(0));exit(0);}else if (pid > 0) {sleep(1);setpgid(pid, pid); //讓子進程自立門戶,成為進程組組長,以它的pid為進程組 id sleep(13);printf("\n");printf("parent PID = %d\n", getpid());printf("parent's parent PID = %d\n", getppid());printf(" parent Group ID = %d\n", getpgid(0));sleep(5);setpgid(getpid(), getppid()); //改變父進程組id為父進程的父進程printf("\n-------Group ID of parent is change to %d\n", getpgid(0));while (1);}return 0; }輸出結果:
17.?getpwnam :獲取登陸用戶相關信息
#include <stdio.h> #include <pwd.h> int main() {struct passwd * pw;char *username = "zxl";pw = getpwnam(username);if (!pw) {printf("%s is not exist\n", username);return -1;}printf("pw->pw_name = %s\n", pw->pw_name);printf("pw->pw_passwd = %s\n", pw->pw_passwd);printf("pw->pw_uid = %d\n", pw->pw_uid);printf("pw->pw_gid = %d\n", pw->pw_gid);printf("pw->pw_gecos = %s\n", pw->pw_gecos);printf("pw->pw_dir = %s\n", pw->pw_dir);printf("pw->pw_shell = %s\n", pw->pw_shell); }19.?snprintf:
#include<stdio.h> #include<stdlib.h>int main() { char str[10]={0};int nLen=snprintf(str,sizeof(str),"123456789012345678");printf("str=%s\n",str);printf("nLen=%d\n",nLen);return 0; }運行結果: str=123456789 nLen=18?
?
?
?
?
總結
- 上一篇: Windows驱动—64位驱动测试64位
- 下一篇: ASSIC标准表