【发布时间】:2015-04-24 01:39:34
【问题描述】:
我正在尝试使用我在网上找到的以下解析功能:
void parse(char *line, char **argv)
{
while (*line != '\0') { /* if not the end of line */
while (*line == ' ' || *line == '\t' || *line == '\n')
*line++ = '\0'; /* replace white spaces with 0 */
*argv++ = line; /* save the argument position */
while (*line != '\0' && *line != ' ' && *line != '\t' &&
*line != '\n')
line++; /* skip the argument until ... */
}
*argv = '\0'; /* mark the end of argument list */
}
这非常适合我想做的事情(制作我自己的 char **argv),但它有一个缺点。它在我的最后一个参数之后和我的 NULL 之前在我的 argv 中创建一个额外的空字符串。你们中的任何人都可以看到我可以在哪里更改代码来解决这个问题吗?我已经尝试过使用 gdb,但我无法弄清楚。
示例:如果我的线路只是"1",*argv =[1, , (null)]。我需要摆脱那个空字符串。
我试过直接改空格,但是不行。例如我试过:
char **temp = args;
int count =0;
while(*temp != NULL){
*temp = *(temp +1);
count++;}
args[count] = NULL;
【问题讨论】:
-
我无法复制您的问题。
-
有趣。我无法想象我的其他代码搞砸了。我所做的只是将文件的一行读入缓冲区。感谢您的尝试!
-
Blank 好像不存在。
-
Check DEMO
[1, NULL] -
如果我的行只是“1”:
"1"-->"1\n"