使用argv进行分段故障[1]

时间:2017-10-02 00:52:48

标签: c

我创建了一个代码,用于根据.txt文件指示的内容从链接列表中插入和删除节点。我之前使用scanf(“%s”,fname)打开文件,但现在我想使用命令行打开它,特别是argv [1] ==文件名来打开它并从中读取。现在我决定使用它,我不断得到分段错误。有没有想过为什么会这样?我刚刚开始在我的大学学习C课程,并且对它很陌生。

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

typedef struct node{
int val; 
struct node *next; 
} Node;

void count_node(Node *head){
     Node *temp;  
      int i=0;
     temp = head;
      while(temp!=NULL){
      i++;
      temp=temp->next;
     }
     printf("number of nodes are %d \n ",i);
  }
 void displayList(Node *head){
    Node *temp;
    temp = head;
    while(temp != NULL){
printf(" %d ",temp->val);
temp = temp->next;
    }
}
Node * findBefore(Node *head,int value){ 
    Node *curr = head; 
    Node *prev = NULL; 
    while(curr != NULL){
       if(value <= curr->val){
            return prev; 
        }
    prev = curr; 
    curr = curr->next; 
    /*printf("a\n");
    break;*/  
}
return prev;     // 1 2 4 
//printf("b\n");
}

void add(Node *head,int newValue){  //return a Node 0 for some reason 
ask TA
    printf("check \n");
    Node *newNode = malloc(sizeof(Node)); 
    if(newNode == NULL){
    printf("check \n");
    return; 
    }
    newNode->val = newValue; 

     //if list is empty: 
    if(head == NULL){ 
        printf("check \n");
        head = newNode; 
    }
    //if list is not empty: 
    Node *prev = findBefore(head,newNode->val); //a -> b 
if(prev == NULL){
    printf("check \n");
    newNode->next = head; 
    head = newNode; 
    return; 
}
printf("check \n");
Node *temp = prev->next; 
prev->next = newNode; 
newNode->next = temp; 
}


void delete(Node *head, int delVal){
    if(head == NULL){
    return; 
 }
Node *prev = NULL; 
Node *curr = head; 

if(delVal == curr->val && prev !=NULL ){ // 1 2
        prev->next = curr->next; 
        free(curr); 
        return; 
    }
    else if(delVal == curr->val && prev == NULL){ // 1
        head = NULL; 
        free(curr); 
        return; 
    }

while(curr != NULL) {

    while(curr != NULL && curr->val != delVal){
        prev = curr; 
        curr = curr->next; 
    }

    if (curr == NULL){
        return; 
    }
    prev->next = curr->next; 
    free(curr);
    curr = prev->next; 
 }
}
int main(int argc,char *argv[]) {   
FILE * fp;
char singleLine[150];  
char command;  
int val;
fp = fopen(argv[1],"r"); 

if(fp == NULL ){
    exit(1); 
}
Node *head = malloc(sizeof(Node)); 
if(head == NULL){ //malloc returns null is there is a memory leak 
    return 1; 
}
while(fgets(singleLine,100,fp)){
    //printf("%s\n",singleLine); 
    sscanf(singleLine,"%c %d",&command,&val); 
    //printf("command character is: %c\n" , command); 
    //printf("The value we have to add is: %d\n",val);
    if(command == 'i'){
        printf("Found i, and add %d\n",val); 
        add(head,val); 
    }
    else if(command == 'd'){
        delete(head,val); 
        printf("found d and delete %d\n",val); 
    }
}
free(head); 
fclose(fp);
printf("\n"); 
count_node(head);  
printf("The linked list is : \n "); 
displayList(head); 
return 0; 
}

1 个答案:

答案 0 :(得分:0)

您用于运行程序的命令是什么?特别是在大型项目中,当我们需要处理外部输入时,最好检查它的有效性。

可能是这样的:

if (argc < 2) { 
  println("Invalid input: No arguments");
  return 1;
}
char *filename = argv[1];
fp = fopen(filename,"r");

查看呼叫失败的原因。尝试在调用argv[1]之前添加此简短的调试代码

println("Number of arguments: %d", argc);
for (int i = 0, i < argc, i++) {
    println("Argument %d: \"%s\"", i, argv[i]);
}