我从文件中读取了一个文本行,我需要将其转换为类似于main函数参数的结构。例如,如果char数组是char* text="There are books in the library."
并且我有以下结构定义:
struct mystruct{
int argc;
char** argv;
};
如果我有struct mystruct a
,功能foo(a, text)
我最终a.argc
等于6,a.argv[0]
等于那里,{等于的{1}} ,...。
C中是否有任何可用于此功能的函数或库?因为当我们执行C main函数时,这个转换是为输入参数自动完成的。
答案 0 :(得分:0)
您可以使用strtok
进行此拆分,如下所示:
struct split_result {
int cnt;
char *buf;
char **strs;
};
int
split(const char *str, struct split_result *rst)
{
int idx, str_num;
char *buf, *sep, **strs;
buf = strdup(str);
if (buf == NULL) {
perror("strdup");
return -1;
}
str_num = 1;
strs = malloc(str_num * sizeof(char *));
if (strs == NULL) {
perror("malloc");
return -1;
}
sep = " \t";
idx = 0;
for (strs[idx] = strtok(buf, sep);
strs[idx];
strs[idx] = strtok(NULL, sep))
{
idx++;
if (idx >= str_num) {
str_num += 10;
strs = realloc(strs, str_num * sizeof(char *));
if (strs == NULL) {
perror("realloc");
return -1;
}
}
}
rst->cnt = idx;
rst->strs = strs;
rst->buf = buf;
return 0;
}
这是一个测试程序及其输出:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct split_result {
int cnt;
char *buf;
char **strs;
};
int
split(const char *str, struct split_result *rst)
{
int idx, str_num;
char *buf, *sep, **strs;
buf = strdup(str);
if (buf == NULL) {
perror("strdup");
return -1;
}
str_num = 1;
strs = malloc(str_num * sizeof(char *));
if (strs == NULL) {
perror("malloc");
return -1;
}
sep = " \t";
idx = 0;
for (strs[idx] = strtok(buf, sep);
strs[idx];
strs[idx] = strtok(NULL, sep))
{
idx++;
if (idx >= str_num) {
str_num += 10;
strs = realloc(strs, str_num * sizeof(char *));
if (strs == NULL) {
perror("realloc");
return -1;
}
}
}
rst->cnt = idx;
rst->strs = strs;
rst->buf = buf;
return 0;
}
int
main(void)
{
int i, j;
struct split_result rst;
const char *msg[] = {
"",
" One",
" One Two",
" One Two Tree ",
"There are books in the library.",
NULL,
};
for (i = 0; msg[i]; i++) {
if (split(msg[i], &rst) < 0) {
exit(EXIT_FAILURE);
}
printf("msg[%d] = >>%s<<\n", i, msg[i]);
for (j = 0; j < rst.cnt; j++) {
printf("cnt = %d: |%s|\n", j, rst.strs[j]);
}
free(rst.strs);
free(rst.buf);
}
exit(EXIT_SUCCESS);
}
输出:
$ ./a.out
msg[0] = >><<
msg[1] = >> One<<
cnt = 0: |One|
msg[2] = >> One Two<<
cnt = 0: |One|
cnt = 1: |Two|
msg[3] = >> One Two Tree <<
cnt = 0: |One|
cnt = 1: |Two|
cnt = 2: |Tree|
msg[4] = >>There are books in the library.<<
cnt = 0: |There|
cnt = 1: |are|
cnt = 2: |books|
cnt = 3: |in|
cnt = 4: |the|
cnt = 5: |library.|
答案 1 :(得分:0)
我假设文件有一个数字,指定参数的数量。如果是这种情况,那么创建一个循环并读取每个字符串,直到遇到分隔符(再次,假设你被告知有一个分隔符)。如果没有“数”参数,则在结构中添加unsigned类型的成员,以跟踪读取的单词数。 你真的不需要创建一个split_function,但如果你决定创建一个split_function,它就不会受到伤害:)