一些C函数的用法(笔记)
来源:互联网 发布:复杂系统控制与优化 编辑:程序博客网 时间:2024/05/16 10:36
(1)fscanf sscanf
FILE *in = fopen ("/proc/uptime", "r");
long uptim = 0, a, b;
if (in)
{
if (2 == fscanf (in, "%ld.%ld", &a, &b))
uptim = a * 100 + b;
fclose (in);
}
------------------------------------------------------------------------------------------------------------------------------
file = fopen("/proc/stat", "r");
if (file == 0)
{
printf("file:/proc/stat not exist/n");
return ;
}
char line[512];
// 获取相关数据
while (line == fgets(line, 512, file))
{
if (4 == sscanf(line, "cpu %lu %lu %lu %lu", &cpuInfo.CpuUser,&cpuInfo.CpuNice,
&cpuInfo.CpuSystem,&cpuInfo.CpuIdel))
{
break;
}
}
(2)strtok 与strtok_
我们有一段字符串 "Fred male 25,John male 62,Anna female 16" 我们希望把这个字符串整理输入到一个struct,
char [25] name ;
char [6] sex;
char [4] age;
}
#include<string.h>
#define INFO_MAX_SZ 255
int main()
{
int in=0;
char buffer[INFO_MAX_SZ]="Fred male 25,John male 62,Anna female 16";
char *p[20];
char *buf=buffer;
while((p[in]=strtok(buf,","))!=NULL) {
buf=p[in];
while((p[in]=strtok(buf," "))!=NULL) {
in++;
buf=NULL;
}
p[in++]="***"; //表现分割
buf=NULL; }
printf("Here we have %d strings/n",i);
for (int j=0; j<in; j++)
printf(">%s</n",p[j]);
return 0;
}
Here we have 4 strings
>Fred<
>male<
>25<
>***<
这只是一小段的数据,并不是我们需要的。但这是为什么呢? 这是因为strtok使用一个static(静态)指针来操作数据,让我来分析一下以上代码的运行过程:
红色为strtok的内置指针指向的位置,蓝色为strtok对字符串的修改
1. "Fred male 25,John male 62,Anna female 16" //外循环
2. "Fred male 25/0John male 62,Anna female 16" //进入内循环
3. "Fred/0male 25/0John male 62,Anna female 16"
4. "Fred/0male/025/0John male 62,Anna female 16"
5 "Fred/0male/025/0John male 62,Anna female 16" //内循环遇到"/0"回到外循环
6 "Fred/0male/025/0John male 62,Anna female 16" //外循环遇到"/0"运行结束。
3. 使用strtok_r
在这种情况我们应该使用strtok_r, strtok reentrant.
char *strtok_r(char *s, const char *delim, char **ptrptr);
相对strtok我们需要为strtok提供一个指针来操作,而不是像strtok使用配套的指针。
代码:
#include<string.h>
#define INFO_MAX_SZ 255
int main()
{
int in=0;
char buffer[INFO_MAX_SZ]="Fred male 25,John male 62,Anna female 16";
char *p[20];
char *buf=buffer;
char *outer_ptr=NULL;
char *inner_ptr=NULL;
while((p[in]=strtok_r(buf,",",&outer_ptr))!=NULL) {
buf=p[in];
while((p[in]=strtok_r(buf," ",&inner_ptr))!=NULL) {
in++;
buf=NULL;
}
p[in++]="***";
buf=NULL; }
printf("Here we have %d strings/n",i);
for (int j=0; jn<i; j++)
printf(">%s</n",p[j]);
return 0;
}
这一次的输出为:
Here we have 12 strings
>Fred<
>male<
>25<
>***<
>John<
>male<
>62<
>***<
>Anna<
>female<
>16<
>***<
让我来分析一下以上代码的运行过程:
红色为strtok_r的outer_ptr指向的位置,
绿色为strtok_r的inner_ptr指向的位置,
蓝色为strtok对字符串的修改
1. "Fred male 25,John male 62,Anna female 16" //外循环
2. "Fred male 25/0John male 62,Anna female 16"//进入内循环
3. "Fred/0male 25/0John male 62,Anna female 16"
4 "Fred/0male/025/0John male 62,Anna female 16"
5 "Fred/0male/025/0John male 62,Anna female 16" //内循环遇到"/0"回到外循环
6 "Fred/0male/025/0John male 62/0Anna female 16"//进入内循环
- 一些C函数的用法(笔记)
- set的一些用法和C中一些函数
- 个人笔记-记录一些小的细节即函数用法
- 一些常见函数的用法
- C++一些函数的用法
- AnsiString 一些函数的用法
- MDX函数的一些用法
- String一些函数的用法
- 《C和指针学习笔记》之sscanf函数的用法
- c语言中rand()函数的用法笔记
- C++:enum的一些用法
- c的一些函数(转)
- 一些自用的c函数
- c语言的一些函数
- c的一些函数(转)
- 关于typedef和指针函数的用法的一些尝试,依然有些不求甚解-C语言
- Python一些函数的笔记
- 一些基础函数的笔记
- 网页设计:css自动换行
- Unix 中的ln详解
- CSS样式切换技巧
- 样式表CSS的高级语法
- 如何用CSS来控制网页背景
- 一些C函数的用法(笔记)
- ASP.NET设计应用程序的七大绝招
- 对OpenGL还是念念不忘
- 视觉设计方法与过程讨论:用理性说话
- sendmessage()和postmessage()使用的注意事项
- 安焦删除贴 牛人纷纷出现(4)
- 中秋节2007中秋节中秋祝福
- c#中String跟string的“区别”
- 沉痛哀悼我的弟弟-恽小华-南理工教授,候选中科院院士——一声叹息