2014-04-17 29 views
0

我有一個程序讀取命令行提示到鏈接列表中添加C程序:使用ARGV,ARGC,讀入鏈表

one two three 

到列表中的命令行。我使用

gcc -o code code.c 

而是一種編譯當我運行

./code one two three 

它也增加了./code到列表的開始第二次提示使其

./codeonetwothree 

當時只有我想要

onetwothree 

任何關於如何編譯而不添加./code到我的鏈表中的建議將不勝感激!

下面是我的代碼,如果需要的話:

#include <stdio.h> 
#include <stdlib.h> 

typedef struct list_node_s{ 
    char the_char; 
    struct list_node_s *next_node; 
}list_node; 

void insert_node(list_node *the_head, char the_char); 
void print_list(list_node *the_head); 

int main(int argc, char *argv[]){ 

    list_node the_head = {'\0', NULL}; 
    int the_count, the_count2; 
    for(the_count = 0; the_count < argc; the_count++){ 
      for(the_count2 = 0; argv[the_count][the_count2] != '\0'; the_count2++){ 
        char next_char = argv[the_count][the_count2]; 
        insert_node(&the_head, next_char); 
      } 
    } 

    print_list(&the_head); 
    int nth_node = 3; 
    printf("Node at the %d spot: ", nth_node); 
    printf("%c \n", the_nth_node(&the_head, nth_node)); 
    return (0); 
} 

void insert_node(list_node *the_head, char the_char){ 

    list_node * current_node = the_head; 
    while (current_node->next_node != NULL) { 
    current_node = current_node->next_node; 
    } 
    current_node->next_node = malloc(sizeof(list_node)); 
    current_node->next_node->the_char = the_char; 
    current_node->next_node->next_node = NULL; 
} 

void print_list(list_node *the_head){ 
    if(the_head == NULL){ 
      printf("\n"); 
    }else{ 
      printf("%c", the_head->the_char); 
      print_list(the_head->next_node); 
    } 

} 
int the_nth_node(list_node* head, int index_of) 
{ 
    list_node* current_node = head; 
    int count_1 = 0; /* the index of the node we're currently 
       looking at */ 
    while (current_node != NULL) 
    { 
    if (count_1 == index_of) 
     return(current_node->the_char); 
    count_1++; 
    current_node = current_node->next_node; 
    } 
} 
+5

開始處理'的argv [0]'是程序的名稱,以便在1'開始the_count'變量 – Alexis

+0

哈哈太簡單。我絕對應該抓住這一點。謝謝! – lchristina26

+0

'the_count = 0' - >'the_count = 1' – BLUEPIXY

回答

1

argv[0]初始化外環計數器是程序的名稱。

如果你不想要的程序的名稱,在argv[1]

for(the_count = 1; the_count < argc; the_count++){ 
1

ARG [0]是該文件的正在執行的名字。所以你應該用1

for(the_count = 1; the_count < argc; the_count++){ 
      for(the_count2 = 0; argv[the_count][the_count2] != '\0'; the_count2++){ 
        char next_char = argv[the_count][the_count2]; 
        insert_node(&the_head, next_char); 
      } 
    }