linux c语言 提取字符串例子说明

linux系统下,提取字符串小例子。

文件a的内容如下:

/test/disk6/cipms_1_v4/000/199/135/2CF001
/test/disk12/cipms_v2/001/286/193/01F001
/test/disk10/cipms_1_v4/000/199/135/2CC001

linux c语言 如何将a的部分内容提取出来,提取的内容如下:

000/199/135/2CF001
001/286/193/01F001
000/199/135/2CC001
#include <stdio.h>
#include <string.h>

int main() {
    FILE *file = fopen("a", "r");
    if (!file) {
        perror("无法打开文件");
        return 1;
    }

    char line[256];
    while (fgets(line, sizeof(line), file)) {
        // 找到 "/cipms" 的位置并向前跳过几个字符,定位到需要的部分
        char *start = strstr(line, "/cipms");
        if (start) {
            // 跳过 "/cipms_x_x/" 部分
            start = strchr(start + 1, '/') + 1;
            if (start) {
                printf("%s", start);
            }
        }
    }

    fclose(file);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值