I have read a text line from a file, and I need to convert it to an structure similar to main function arguments. For example if the char array is char* text="There are books in the library."
and I have below structure definition:
struct mystruct{
int argc;
char** argv;
};
if I have struct mystruct a
, with the function foo(a, text)
I end up with a.argc
equal to 6, a.argv[0]
equal to There , a.argv[1]
equal to are , ... .
Is there any function or library in CI can use for this purpose? Since this conversion is done automatically for input arguments when we execute C main function.
You could use strtok
to do this split, like this:
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;
}
Here is a test program and its output:
#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);
}
Output:
$ ./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.|
I presume the file have a number that specifies the number of arguments. if that is the case then create a loop and read each string until you encounter a delimiter ( again, assuming that you were told there is one ). If you don't have "the" number of arguments then add a member of type unsigned, in your struct, to keep track of the number of words read. You really don't need to create a split_function, but it doesn't hurt if you decided to create one :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.