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;
}