c - 使用 argv[1] 的段错误

标签 c

我创建了一个代码,根据 .txt 文件的指示从链接列表中插入和删除节点。我以前使用 scanf("%s",fname) 打开文件,但现在我想使用命令行打开它,特别是 argv[1] == 文件名来打开它并从中读取。现在我决定使用它,但我不断遇到段错误。有什么想法为什么会发生这种情况吗?我刚刚开始在大学学习 C 类(class),对它还是个新手。

#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; 
}

最佳答案

您用来运行该程序的命令是什么?特别是在较大的项目中,当我们需要处理外部输入时,检查其有效性是一个很好的做法。

也许是这样的:

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]);
}

关于c - 使用 argv[1] 的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46518284/

相关文章:

c - 为什么 strtok() 不从行中选择 '?' 字符?

java - SWIG,我可以为从 Java 传递到 C 的 char** 赋值吗

c - For循环忽略第一行,在C中第一次循环执行后

c - 理解 c 中的字符串数组

将字符数组转换为整数数组

c - __func 函数的约定是什么?

c++ - 为什么我在 Linux 上看不到 SOCK_SEQPACKET 的 MSG_EOR?

c - 为什么两个指针的地址是连续的?

c - 特定有限整数集的高效映射

检查缓存中是否存在 Kerberos 票证