您的位置:首页 > 运维架构 > Linux

嵌入式成长轨迹16 【Linux应用编程强化阶段】【Linux下的C编程下】【Linux c基本应用】

2012-03-09 16:58 435 查看
一 字符串操作
应用程序按其功能可以分为数值计算、非数值计算以及输入输出操作等。非数值计算程序占相当大的比例,其核心就是字符串的处理。

1 字符测试
1).测试字符是否为英文字母

int isalpha(int c)

/* example1.c */
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[] = "A13F+2c#D";            /* 定义字符数组 */
int i;
for (i=0; str[i]!=0; i++)                /* 遍历数组中的每个字符 */
{
if (isalpha(str[i]))                    /* 测试字符是否为英文字母 */
{
printf("%d : %c \n", i, str[i]);
}
}
return 0;
}


2).测试字符是否为数字
int isdigit(int c)

/* example2.c */
#include <stdio.h>
#include <ctype.h>
int main()
{
int c;
printf("Input the characters :\n");
for(;;)
{
c = getchar();                    /* 从键盘获取字符 */
printf("%c : %s digit\n", c, isdigit(c)?"is":"not");        /* 测试字符是否为数字 */
}
return 0;
}


2 字符串初始化
在C语言中,字符串被当作字符数组来处理,对应于内存中的一块连续区域。

void *memset(void *buffer, int c, int count);

/* example3.c */
#include <stdio.h>
#include <string.h>
int main()
{
char s[] = "Hello World";
printf("%s\n", s);
memset(s, 'H', 5);     /* 将字符串的前5个字节设为字符H */
printf("%s\n", s);
return 0;
}


3 字符串复制
字符串复制的函数主要有strcpy、strdup、memcpy以及memmove等,下面分别进行介绍。

1).strcpy函数

char *strcpy(char *dest,char *src);

2).strdup函数

char *strdup(char *s);

3).memcpy函数

void *memcpy(void *dest, void *src,unsigned int count);

/* example4.c */
#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 128
int main()
{
char s[] = "Linux C Programming";
char d[BUFFER_SIZE];    /* 定义内存缓冲区 */
strcpy(d, s);       /* 字符串拷贝 */
printf("%s\n", d);
return 0;
}


4).memmove函数

void *memmove(void *dest, const void *src,size_t n);

/* example5.c */
#include <stdio.h>
#include <string.h>
int main()
{
char s[] = "Linux C Programming";
printf("%s\n", s);
memmove(s, s+6, strlen(s)-6);  /* 字符串拷贝,这里源指针和目标指针指向的内存区域有重叠 */
s[strlen(s)-6] = '\0';     /* 字符串结束符 */
printf("%s\n", s);
return 0;
}


4 字符串比较
字符串比较的函数主要有strcmp、strncmp、strcasecmp、strncasecmp以及memcmp等,下面分别进行介绍。

1).strcmp函数

int strcmp(const char *s1, const char *s2);

/* example6.c */
#include <stdio.h>
#include <string.h>
int main()
{
char s1[] = "Hello World";
char s2[] = "Hello world";
int ret;
printf("s1 : %s\n", s1);
printf("s2 : %s\n", s2);
ret = strcmp(s1, s2);    /* 比较两个字符串 */
if(ret == 0)
printf("s1 and s2 are identical\n");
else if(ret < 0)
printf("s1 less than s2\n");
else
printf("s1 greater than s2\n");
return 0;
}


2).strncmp函数
int strncmp(const char *s1, const char *s2,size_t n);

3).strcasecmp/strncasecmp函数 忽略大小写
int strcasecmp(const char *s1, const char *s2);
int strncasecmp(const char *s1, const char *s2,size_t n);

4).memcmp函数

int memcmp(const void *s1, const void *s2,size_t n);

5 字符/字符串查找
字符/字符串查找的函数主要有index、rindex、strchr、strrchr以及strstr等,下面分别进行介绍。

1).index/rindex函数 返回找到字符的指针(此时输出就会输出它和它后面的各个字符,直至到'\0')
char *index(const char *s, int c);
char *rindex(const char *s, int c); 从后面找起

/* example7.c */
#include <stdio.h>
#include <string.h>
int main()
{
char s[] = "Hello World";
char *p;
p = index(s, 'W');     /* 在字符串s中查找字符W */
printf("%s\n", p);
return 0;
}


2).strchr/strrchr函数

char *strchr(const char *s, int c);
char *strrchr(const char *s, int c);

3).strstr函数
char *strstr(const char *haystack,const char *needle);

/* example8.c */
#include <stdio.h>
#include <string.h>
int main()
{
char haystack[] = "Linux C Programming";
char needle[] = "Pro";
char *p;
p = strstr(haystack, needle);   /* 查找字符串 */
if(p != NULL)
printf("%s\n", p);
else
printf("Not Found!\n");
return 0;
}


6 字符串连接与分割
实现字符串连接与分割的函数为strcat、strncat和strtok,下面分别进行介绍。

1).strcat函数
char *strcat(char *dest, const char *src);

/* example9.c */
#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 64
int main()
{
char s[BUFFER_SIZE] = "orld";
char d[BUFFER_SIZE] = "Hello W";
strcat(d, s);      /* 连接字符串 */
printf("%s\n", s);      /* 输出源字符串 */
printf("%s\n", d);      /* 输出目标字符串 */
return 0;
}


2).strncat函数

char *strncat(char *dest, const char *src,size_t n);
3).strtok函数

char *strtok(char *str, const char *delim);

在str中找出以delim中的字符为分隔的字符串,即是源串中除去了含有分隔串中的所有字符后余下的一段段的字符串,每调用一次找到一串,找不到则返回空串。第一次调用必须传给它有效的字符串,第二次传NULL就可以了,每次调用返回找到的子串的时候都会把源串中该子串的尾部字符(原来是搜索串中的某一字符)修改成'/0'字符返回值为每次调用得到的字串。

/* example10.c */
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Linux C Programming";
char *p;
p = strtok(str, " ");     /* 以空格对字符串进行分割 */
while(p != NULL)
{
printf("%s\n", p);     /* 输出分割后的子串 */
p = strtok(NULL, " ");
}
printf("str : %s\n", str);
return 0;
}


二 数据转换
数据转换包括英文字母大小写之间的转换、字符串与整数、浮点数之间的转换,下面分别进行介绍。

1 字母大小写转换
int toupper(int c);
int tolower(int c);

/* example11.c */
#include <stdio.h>
#include <ctype.h>
int main()
{
char s[] = "Hello World!";
int i;
printf("%s\n", s);      /* 输出原来的字符串 */
for(i=0; i<sizeof(s); i++)
{
s[i] = toupper(s[i]);     /* 全部转换为大写字母 */
}
printf("%s\n", s);      /* 输出转换后的字符串 */
for(i=0; i<sizeof(s); i++)
{
s[i] = tolower(s[i]);     /* 全部转换为小写字母 */
}
printf("%s\n", s);      /* 输出转换后的字符串 */
return 0;
}


二 字符串转换
实现字符串与整数、浮点数之间转换的函数有atoi、atol、strtol、atof、strtod以及gcvt等,下面分别进行介绍

1.将字符串转换为整数

int atoi(const char *nptr);

long atol(const char *nptr);

/* example12.c */
#include <stdlib.h>
#include <stdio.h>
int main()
{
char a[] = "-100";
char b[] = "0x20";     /* 转换后结果为0,因此只转换到字符x */
int c;
c = atoi(a) + atoi(b);
printf("c = %d\n", c);
return 0;
}


2.将字符串转换为浮点数

double atof(const char *nptr);

3.将浮点数转换为字符串
char *gcvt(double number, size_t ndigits,char *buf);

/* example14.c */
#include <stdlib.h>
#include <stdio.h>
#define BUFFER_SIZE 64
int main()
{
double a=123.456;
char str[BUFFER_SIZE];    /* 定义缓冲区 */
gcvt(a, 5, str);      /* 将浮点数转换为字符串,显示5位 */
printf("%s\n",str);
gcvt(a, 6, str);      /* 将浮点数转换为字符串,显示6位 */
printf("%s\n",str);
return 0;
}


4.将字符串根据参数base来转换成长整型数
long int strtol(const char *nptr,char **endptr,int base);

/* example13.c */
#include <stdlib.h>
#include <stdio.h>
int main()
{
char a[] = "10000";
char b[] = "20000";
char c[] = "FFFF";
printf("a = %ld\n", strtol(a, NULL, 2));   /* 二进制 */
printf("b = %ld\n", strtol(b, NULL, 10));    /* 十进制 */
printf("c = %ld\n", strtol(c, NULL, 16));    /* 十六进制 */
return 0;
}


三 内存分配与释放
1 内存空间的分配

void *alloca(unsigned size);

void *malloc(unsigned size);
void *calloc(size_t nmemb, size_t size);

2 内存空间的释放

void free(void *ptr);

/* example15.c */
#include <stdlib.h>
#include <stdio.h>
#define SIZE 10
int main()
{
int *p;
int i, sum;
p = (int *)malloc(SIZE*sizeof(int));  /* 分配内存空间 */
if(p == NULL)      /* 如果内存空间分配失败,输出错误信息 */
{
printf("malloc error\n");
}
else
{
sum = 0;
for(i=0; i<SIZE; i++)
{
*(p+i) = i;     /* 对内存单元的引用*(p+i)等价于p[i] */
sum += p[i];
}
free(p);       /* 释放内存空间 */
}
printf("sum = %d\n", sum);
return 0;
}


3 更改已分配的内存空间

void *realloc(void *ptr, size_t size);

四 时间和日期
Linux系统下的应用程序设计过程中,经常会涉及时间和日期的处理

1 时间和日期的获取
time和gettimeofday用来获取系统当前的时间和日期,下面分别进行介绍。

1).time函数
time_t time(time_t *t);2).gettimeofday函数

int gettimeofday(struct timeval *tv,struct timezone *tz);

struct timeval
{

long tv_sec;
long tv_usec;
};

/* example16.c */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
int main()
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);    /* 获取当前的时间 */
printf("tv_sec : %ld\n", tv.tv_sec);
printf("tv_usec : %ld\n", tv.tv_usec);
printf("tz_minuteswest : %d\n", tz.tz_minuteswest);
printf("tz_dsttime : %d\n", tz.tz_dsttime);
return 0;
}


2 时间和日期的显示
ctime、gmtime、asctime函数用来将时间和日期转换为字符串格式,下面分别进行介绍。

1).ctime函数

char *ctime(const time_t *timep);

/* example17.c */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{
time_t timep;
time(&timep);      /* 获取当前的时间 */
printf("%s", ctime(&timep));   /* 转换为字符串格式并输出 */
return 0;
}


2).gmtime函数

struct tm*gmtime(const time_t *timep);
struct tm { int tm_sec;
int tm_min;
int tm_hour;

int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;

};

/* example18.c */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{
time_t timep;
struct tm *p;
char *wday[]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
time(&timep);      /* 获取当前的时间 */
p = gmtime(&timep);     /* 转换为tm结构 */
printf("%d/%d/%d\n", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday);  /* 输出日期 */
printf("%s\n", wday[p->tm_wday]);         /* 输出星期 */
printf("%d:%d:%d\n", p->tm_hour, p->tm_min, p->tm_sec);     /* 输出时间 */
return 0;
}


3).asctime函数

char *asctime(const struct tm *timeptr);

3 时间的计算
double difftime(time_t time1, time_t time0);

/* example19.c */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#define SIZE 100000
int main()
{
time_t time0;
time_t time1;
int i, j;
int *p;
double d;
time(&time0);      /* 获取循环运行前的时间 */
for(i=0; i<10000; i++)
{
p = (int *)malloc(SIZE*sizeof(int));  /* 分配内存空间 */
for(j=0; j<SIZE; j++)
{
*(p+j) = j;     /* 初始化 */
}
free(p);       /* 释放内存空间 */
}
time(&time1);      /* 获取循环运行后的时间 */
d = difftime(time1, time0);   /* 计算时间差 */
printf("%f\n", d);
return 0;
}


五 其他应用
其他应用包括命令行参数分析、用户和用户组操作以及环境变量设置等,下面分别进行介绍。

1 命令行参数分析
main函数
int main(int argc, char **argv)
或:

int main(int argc, char *argv[])

/* example20.c */
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char ** argv)
{
int i;
for (i=0; i<argc; i++)
{
printf("Argument %d is %s.\n", i, argv[i]);  /* 输出传入main函数的各个参数 */
}
return 0;
}


getopt函数可以用来分析传入的各命令行参数

int getopt(int argc, char *const argv[], const char *optstring);
单个字符,表示选项;
单个字符后面跟一个冒号:
表示该选项后面必须跟一个参数,参数紧跟在选项后面,或者以空格隔开;

单个字符后面跟两个冒号:表示该选项后面必须跟一个参数,参数必须紧跟在选项后面,不能以空格隔开。

/* example21.c */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char * argv[])
{
int ch;
if(argc < 2)
{
printf("no arguments\n");
}
while ((ch = getopt(argc, argv, "ab:c")) != -1)   /* 分析各选项 */
{
printf("optind : %d\n", optind);     /* 输出全局变量optind的值 */
switch (ch)
{
case 'a':        /* 选项a */
printf("option : a\n");
break;
case 'b':        /* 选项b,该选项必须带参数 */
printf("option : b\n");
printf("argument of -b : %s\n", optarg);
break;
case 'c':        /* 选项c */
printf("option : c");
break;
case '?':        /* 其他选项 */
printf("unknown option : %c\n", (char)optopt);
break;
default:
;
}
}
return 0;
}


2 用户和用户组操作
用户和用户组操作的函数有getuid、getgid、getlogin、getpwent、getpwnam以及getutent等。

1).getuid函数
uid_t getuid(void);

2).getgid函数
gid_t getgid(void);

3).getlogin函数

char *getlogin(void);

/* example22.c */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
uid_t uid;
gid_t gid;
uid = getuid();     /* 获取当前用户的标识符 */
gid = getgid();     /* 获取当前组的标识符 */
printf("user name : %s\n", getlogin()); /* 输出当前用户的账号名称 */
printf("uid = %d\n", uid);
printf("gid = %d\n", gid);
return 0;
}


4).getpwent函数

struct passwd *getpwent(void);struct passwd {

char *pw_name;
char *pw_passwd;
uid_t pw_uid;
gid_t pw_gid;
char *pw_gecos;
char *pw_dir;
char *pw_shell;
};

/* example23.c */
#include <stdlib.h>
#include <stdio.h>
#include<pwd.h>
#include<sys/types.h>
int main()
{
struct passwd *user;
user = getpwent();
while(user != NULL)    /* 读取用户账号信息 */
{
printf("%s:%d:%d:%s:%s:%s\n", user->pw_name, user->pw_uid, user->pw_gid,
user->pw_gecos, user->pw_dir, user->pw_shell);
user = getpwent();
}
endpwent();      /* 关闭文件 */
return 0;
}


5).getpwnam函数

struct passwd getpwnam(const char * name);

6).getutent函数

struct utmp *getutent(void);struct utmp {

short int ut_type;
pid_t ut_pid;
char ut_line[UT_LINESIZE];

char ut_id[4];
char ut_user[UT_NAMESIZE];

char ut_host[UT_HOSTSIZE];

struct exit_status ut_exit;

long int ut_session;

struct timeval ut_tv;
int32_t ut_addr_v6[4];
char __unused[20];

};

/* example24.c */
#include <stdlib.h>
#include <stdio.h>
#include <utmp.h>
int main()
{
struct utmp *u;
while((u = getutent()) != NULL)
{
if(u->ut_type == USER_PROCESS) /* 判断是否为普通用户进程 */
printf("%s\t%s \n", u->ut_user, u->ut_line);
}
endutent();      /* 关闭文件 */
return 0;
}


3 环境变量操作
环境变量是包含系统环境信息的字符串,可以作用于用户的整个工作空间。这一小节将介绍环境变量操作相关的三个函数:getenv、putenv和setenv,他们分别用来获取环境变量、更改或增加环境变量。

1).getenv函数
char *getenv(const char *name);

/* example25.c */
#include <stdlib.h>
#include <stdio.h>
int main()
{
char *p;
p = getenv("SHELL");    /* 获取环境变量 */
if(p == NULL)     /* 如果环境变量获取失败,输出错误信息并退出 */
{
printf("getenv error\n");
exit(1);
}
else
printf("SHELL=%s\n", p);    /* 输出环境变量的内容 */
return 0;
}


/* example26.c */
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char * argv[])
{
char * p;
if(argc != 3)       /* 检查命令行参数个数 */
{
printf("no arguments\n");
exit(1);
}
if((p=getenv(argv[1])) == NULL)   /* 获取并输出环境变量 */
printf("%s does not exist\n", argv[1]);
else
printf("%s=%s\n", argv[1], p);
setenv(argv[1], argv[2], 1);    /* 设置环境变量 */
if((p=getenv(argv[1])) != NULL)   /* 重新获取并输出环境变量 */
{
printf("%s=%s\n", argv[1], p);
}
return 0;
}


2).putenv函数

int putenv(const char *string);

3).setenv函数
int setenv(const char *name,const char * value, int overwrite);

六 常见面试题
常见面试题1:memcpy函数与strcpy函数的主要差别是什么?

常见面试题2:编写一个函数,求两个给定字符串的最大公共子串。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐