C语言基础 第九章 常用字符串操作函数

sscanf():可以将一个长的字符串按照需求分割成想要的格式

实例:

#include<stdio.h>
#include<string.h>
int main()
{
    //1. 取指定长度的字符串
    char str[100];
    sscanf("12345","%4s",str);
    printf("%s\n",str);//1234

    //2. 格式化时间
    int year, month, day, hour, minute, second;
    sscanf("2013/02/13 14:55:34","%d/%d/%d %d:%d:%d",&year, &month, &day, &hour, &minute, &second);
    printf("time=%d-%d-%d %d:%d:%d\n", year, month, day, hour, minute, second);//time=2013-2-13 14:55:34

    //3. %*d和%*s加了(*)表示跳过此数据不读入(也就是不把此数据读入参数中)
    sscanf("1234abcd","%*d%s",str);//此处表示跳过 %d 的数据,也就是跳过 :1234
    printf("%s\n",str);// abcd

    //4. 取到指定字符为止的字符串。如例子所示,遇到‘+’为止的字符串
    sscanf("1234+abc","%[^+]",str);
    printf("%s\n",str);// 1234

    //5.  取到指定字符集为止的字符串。如遇到小写字母为止的字符串。
    sscanf("1234+abc1234","%[^a-z]",str);
    printf("%s\n",str);// 1234+

    //6. 取仅包含指定字符集的字符串。(取仅包含数字和小写字母的字符串,是取得连续的字符串)。
    sscanf("123456abcdefBFRGTY7890","%[1-9a-z]",str);
    printf("%s\n",str);// 123456abcdef

    return 0;
}    

实际应用:

读取 pclist_info 文件内容 ,90:2b:34:67:2b:43 [192.168.10.111] [] [] [0 1900/01/00 00:00:00] [Wire] [STATIC] [Online] [] [] [Used]

此函数遇到一个问题,当遇到, [] 第一个符号时,里面是空的,后面的就无法获取了

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>

typedef struct {
    char hostClass[8];
    char hostMAC[20];
    char hostDHCPName[70];
    char hostDevName[70];
    char hostIP[20];
    char hostConnectInterface[8];
    char hostOnlineTime[64];
    char hostStotageAccessStatus[8];
}CMCCLanHostInfo;

    int main(void)
    {
        FILE *fp = NULL;
        char line[512] = {0};
        int i=0;
        struct tm timeinfo={0};
        CMCCLanHostInfo tmp_lhi={0};
        char if_mac[20]={0};
        char contype[20]={0};
        char conssid[20]={0};
        char ip_addressing[20]={0};
        char time[128] ="";
        char *p = NULL;
        char dest[] = "[]";
        if ((fp = fopen("pclist_info", "r")) == NULL) {
            printf("%s", "Cannot open pclist_info file!\n");
            return 0;
        }
        if(fgets(line, sizeof(line)-1 , fp) != NULL )
        {
            line[strlen(line)-1] = ‘\0‘;

// 90:2b:34:67:2b:43 [192.168.10.111] [0] [1] [0 1900/01/00 00:00:00] [Wire] [STATIC] [Online]
            sscanf(line, "%[^]%*[^[][%[^]] %*[^[][%[^]] %*[^[][%[^]] %*[^[][%[^]] %*[^[][%[^]] %*[^[][%[^]] %*[^[][%[^]] %*[^[][%[^]] %*[^[][%[^]] %*[^[][%[^]]",
                        tmp_lhi.hostMAC,
                        tmp_lhi.hostIP,
                        tmp_lhi.hostDevName,
                        tmp_lhi.hostDHCPName,
                        time,
                        contype,
                        ip_addressing,
                        tmp_lhi.hostClass,
                        tmp_lhi.hostConnectInterface,
                        conssid,
                        tmp_lhi.hostStotageAccessStatus);
            printf("hostMAC = %s \n",tmp_lhi.hostMAC);
            printf("hostIP = %s \n",tmp_lhi.hostIP);
            printf("hostDevName = %s \n",tmp_lhi.hostDevName);
            printf("hostDHCPName = %s \n",tmp_lhi.hostDHCPName);
            printf("time = %s \n",time);
            printf("contype = %s \n",contype);
            printf("ip_addressing = %s \n",ip_addressing);
            printf("hostClass = %s \n",tmp_lhi.hostClass);
            printf("hostConnectInterface = %s \n",tmp_lhi.hostConnectInterface);
            printf("conssid = %s \n",conssid);
            printf("hostStotageAccessStatus = %s \n",tmp_lhi.hostStotageAccessStatus);

        }

        fclose(fp);
        return 0;
    }

原文地址:https://www.cnblogs.com/vx-cg248805770/p/12128759.html

时间: 2024-08-29 15:23:52

C语言基础 第九章 常用字符串操作函数的相关文章

C语言的常用字符串操作函数(一)

一直做的是单片机相关的程序设计,所以程序设计上更偏向底层,对于字符串的操作也仅限于液晶屏幕上的显示等工作,想提高下字符串操作的水平,而不是笨拙的数组替换等方式,翻看帖子发现C语言的字符串操作函数竟然这样丰富而实用,在此记录,已备后用. No.1 strlen():字符串长度计算函数 应用实例: 1 #include<stdio.h> 2 #include<string.h> 3 4 char TextBuff[] = "Hello_My_Friend!"; 5

mysql常用字符串操作函数大全

测试表 CREATE TABLE `string_test` ( `id` int(11) NOT NULL auto_increment COMMENT '用户ID', `name` varchar(50) NOT NULL default '' COMMENT '名称', `job` varchar(23) NOT NULL COMMENT '工作', `sex` tinyint(1) NOT NULL default '1' COMMENT '性别', `hobby` varchar(10

mysql常用字符串操作函数大全,以及实例

今天在论坛中看到一个关于mysql的问题,问题如下 good_id     cat_id12654         665,56912655         601,4722 goods_id是商品idcat_id是分类id当我,怎么根据这种分类ID查数据(一个商品有多个分类,而且用逗号隔开了)我现在用的是like 这样的话,输入一个分类id是688,或者4722都能出来这个商品,但输入一个722也出来这个商品了. 如果用like做的话,肯定会有问题的,我的开始的想法是,把cat_id里面的字符

C语言常用字符串操作函数总结

函数名: strcpy 功 能: 将参数src字符串拷贝至参数dest所指的地址 用 法: char *strcpy(char *dest, const char *src); 返回值: 返回参数dest的字符串起始地址 说 明: 如果参数dest所指的内存空间不够大,可能会造成缓冲溢出的错误情况,在编写程序时需特别留意,或者用strncpy()来取代: 程序例: #include <stdio.h> #include <string.h> int main(void) { cha

【阅读笔记】《C程序员 从校园到职场》第六章 常用文件操作函数 (Part 1)

参考链接:https://blog.csdn.net/zhouzhaoxiong1227/article/details/24926023 让你提前认识软件开发(18):C语言中常用的文件操作函数总结及使用方法演示代码 - CSDN博客 Contents: 1. C语言中常用的文件操作函数总结 (1) fopen   作用:打开文件 (2) fclose  作用:关闭文件 (3) fgetc   作用:由文件中读取一个字符. (4) fgets   作用:由文件中读取一字符串. (5) fflu

PHP常用字符串操作函数实例总结(trim、nl2br、addcslashes、uudecode、md5等)

/*常用的字符串输出函数 * * echo() 输出字符串 * print() 输出一个或多个字符串 * die() 输出一条信息,并退出当前脚本 * printf() 输出格式化字符串 * sprintf() 把格式化的字符串写入到一个变量中 * */ //ucfirst //将字符串中的首字母转换为大写 $str="string"; echo ucfirst($str); echo "<hr><br/>"; //ucwords() //将

Hive常用字符串操作函数

? ? 返回类型 函数名 描述 int length(string A) 返回字符串A的长度 select length(weoirjewo);select length(name) from stu; 此函数在实际工作,可以用于校验手机号,身份号等信息的合法性 string reverse(string A) 返回字符串A的反转结果 select reverse('abcd');select length(name) from stu; string concat(string A, stri

梓益C语言学习笔记之常用字符串操作(sscanf &amp; strtok)

一.sscanf int sscanf(const char *buf,const char *format, -);  \\从buf指定的内存区域中读入信息 例: int a, b, c; sscanf("2013:10:1", "%d:%d:%d", &a, &b, &c); printf("%d  %d  %d\n",a,b,c); sscanf()的正则表达式是从头向后顺序查找匹配字符,找不到就返回错误,如下面的几

梓益C语言学习笔记之常用链表操作函数

一.创建链表 void link_creat_head(STU **p_head,STU *p_new) { STU *p_mov=*p_head; if(*p_head==NULL) //当第一次加入链表为空时,head执行p_new { *p_head=p_new; p_new->next=NULL; } else //第二次及以后加入链表 { while(p_mov->next!=NULL) { p_mov=p_mov->next; //找到原有链表的最后一个节点 } p_mov-