開発環境-アプリケーションレイヤーは文字列を解析し、データを抽出します

アプリケーション層は、ioctlを介してカーネル内の情報を取得できます。

実際、カーネル層でファイルにデータを書き込み、アプリケーション層でファイルを解析することもできます。

 

C言語で文字列を解析する方法は次のとおりです。

https://www.cnblogs.com/yi-meng/p/3620244.html

 

ファイルを解析する方法は次のとおりです。

static int g_pcie_ep_fd = -1;
int BrdGetEPStatus(int *status)
{
	int fd;
	int ret = -1;
	char *tmp;
	int len;
	char *line;
	char *saveptr = NULL;
	char *key;
	char *value;

	tmp = (char *)malloc(1024);
	if (tmp == NULL)
		return ret;

	if (g_pcie_ep_fd >= 0) 
		goto go_on;

	fd = open("/proc/pcie_ep", O_RDONLY);
	if (fd < 0)
	{
		perror("open");
		return fd;
	}
	g_pcie_ep_fd = fd;

go_on:
	memset(tmp, 0x0, 1024);
	len = read(g_pcie_ep_fd, tmp, 1024);
	printf("BrdGetEPStatus: %s\n", tmp);

	line = strtok_r(tmp, "\n", &saveptr);
	while (line != NULL) {
		/* line: "KEY=value" */
		key = line;
		value = strchr(line, '=');
		if (!value) {
			goto next_line;
		}
		*value = '\0';
		value++;
		if (strcmp(key, "devCount") == 0) {
			sscanf(value, "%d", status);
			printf("strcmp(key, devCount: %d\n", *status);
		}
	
next_line:
		line = strtok_r(NULL, "\n", &saveptr);
	}

	free(tmp);

	return 0;
}

 

おすすめ

転載: blog.csdn.net/Ivan804638781/article/details/100023391