首页 > 解决方案 > 警告和分段错误核心转储

问题描述

这是一个删除文件中特定行的程序。它复制所需的行并将其打印到同一目录中的另一个文件中。除了诸如incompatible pointer type [-Wincompatible-pointer-types]. 当我运行代码时,我也得到了prtintf语句,但是当我输入 input 时Segmentation fault (core dumped)。它与警告有关还是其他原因?

代码

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


char *name_find(char *buf[], char *name[]) {
    const char *p = NULL;
    size_t len = strlen(name);

    if (len > 0) {
        for (p = buf ; (p = strstr(p, name)) != NULL; p++) {
            if (p == buf || !isalnum((unsigned char)p[-1])) {
                if (!isalnum((unsigned char)p[len]))
                    break;  /* we have a match! */
                p += len;   /* next match is at least len+1 bytes away */ 
            }
        }
    }
    return p;
}


int main()
{
    char name[25];
    char buf[100];
        setenv("PFILE","/home/ashwin/Desktop/FILE/",1);
        char ori_path[100],new_path[100];
    if (!getenv("PFILE")){
    }
    else{
       strcpy(ori_path, getenv("PFILE"));
       strcpy(new_path, getenv("PFILE"));
       strcat(ori_path, "shadow");
       strcat(new_path, "shadow1");
       }

    bool success=false;
    printf("Enter the command\n ");
    printf("userdel ");

    FILE *fold = fopen(ori_path, "r"); // old file
    FILE *fnew = fopen(new_path, "w"); // new temp file

    fgets(name,25,stdin);
    for(int i = 0; i < strlen(name); i++)
    {
       if(name[i] == '\n')
       {
          name[i] = '\0';
          break;
       }
    }

    while (fgets(buf, 100, fold)) {
    // read lines until error or EOF
        if (!name_find(buf, name)) {
        fprintf(fnew, "%s", buf);
        success=true;
        }

    }

    if(success){
        printf("Success !!!\n");
    }

    return 0;
} 

标签: csegmentation-fault

解决方案


char *name_find(char *buf[], char *name[])

您使用char *buf[],这意味着buf是指向 的指针数组char,而不是指向 的指针char。改为使用char* buf。也一样name


此外:

    FILE *fold = fopen(ori_path, "r"); // old file
    FILE *fnew = fopen(new_path, "w"); // new temp file

您应该通过检查返回的指针是否为空指针来检查文件流的打开是否成功:

    FILE *fold = fopen(ori_path, "r"); // old file
    if(!fold)
    {
        fputs("Error at opening fold!", stderr);
        exit(1);
    }

    FILE *fnew = fopen(new_path, "w"); // new temp file
    if(!fnew)
    {
        fputs("Error at opening fnew!", stderr);
        exit(1);
    }

试试这个代码:

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


char *name_find(char *buf, char *name) {
    const char *p = NULL;
    size_t len = strlen(name);

    if (len > 0) {
        for (p = buf ; (p = strstr(p, name)) != NULL; p++) {
            if (p == buf || !isalnum((unsigned char)p[-1])) {
                if (!isalnum((unsigned char)p[len]))
                    break;  /* we have a match! */
                p += len;   /* next match is at least len+1 bytes away */ 
            }
        }
    }
    return p;
}


int main (void)
{
    char name[25];
    char buf[100];
        setenv("PFILE","/home/ashwin/Desktop/FILE/",1);
        char ori_path[100],new_path[100];
    if (!getenv("PFILE")){
    }
    else{
       strcpy(ori_path, getenv("PFILE"));
       strcpy(new_path, getenv("PFILE"));
       strcat(ori_path, "shadow");
       strcat(new_path, "shadow1");
       }

    bool success=false;
    printf("Enter the command\n ");
    printf("userdel ");

    FILE *fold = fopen(ori_path, "r"); // old file
    if(!fold)
    {
        fputs("Error at opening fold!", stderr);
        exit(1);
    }

    FILE *fnew = fopen(new_path, "w"); // new temp file
    if(!fnew)
    {
        fputs("Error at opening fnew!", stderr);
        exit(1);
    }

    fgets(name,25,stdin);
    for(unsigned int i = 0; i < strlen(name); i++)
    {
       if(name[i] == '\n')
       {
          name[i] = '\0';
          break;
       }
    }

    while (fgets(buf, 100, fold)) {
    // read lines until error or EOF
        if (!name_find(buf, name)) {
        fprintf(fnew, "%s", buf);
        success=true;
        }

    }

    if(success){
        printf("Success !!!\n");
    }

    return 0;
} 

推荐阅读