无法在C中找到导致分段错误(核心转储)的原因

时间:2014-09-07 15:21:58

标签: c

我正在尝试获取用户输入,将输入写入文件,然后使用数据作为结构。文件编译正常但在第一次输入后我收到此错误消息:

$ gcc student.c -o student  
$ ./student  
Student First Name:  
alex  
Student Last Name:  
Segmentation fault (core dumped)

代码:

/* student.h is to include the structure for stundent info */
#include <stdio.h>
#include <stdlib.h>

typedef struct {  
    char sID[9];
    char firstN[30];
    char lastN[30];
    }student[100];

#include "student.h"    
int main()
{
    FILE *sIf;
    int i;
    int num;
    student *sI;
    char *c;
    char *b;
    char *a;

    sIf = fopen("student.txt", "a+");
    if(!sIf)
    {
        printf("File could not be opened\n\a\a");
        getchar();
        return -1;
    }
    {
        printf("Student First Name: \n");
        scanf("%s", b);
        printf("Student Last Name: \n");
        scanf("%s", a);

        printf("Student ID Number: \n");
        scanf("%s", c);

        fprintf(sIf, "%s, %s\t%s\n", a, b, c);
    }

    fclose(sIf);
    return 0;
}

1 个答案:

答案 0 :(得分:2)

  1. 编码时始终使用适当的缩进,这使代码易于阅读。
  2. 您没有为char指针a, b, c分配内存。
  3. 而不是scanf()使用fgets()来接受输入字符串。
  4. 尝试使用此固定代码(我已在必要时添加了注释)。

    /* student.h is to include the structure for stundent info */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    //#include "student.h"
    typedef struct {
        char sID[9];
        char firstN[30];
        char lastN[30];
        }student[100];
    int main()
    {
        FILE *sIf;
        student *sI;//Why are you using it?
        char *ptr_pos;
        //Fix 1 Allocate space for a, b and c
        char *c=malloc(sizeof(char)*256);
        if(c==NULL)
        {
            puts("Failed to allocate memory for variable c");
            exit(1);
        }
        char *b=malloc(sizeof(char)*256);
        if(b==NULL)
        {
            puts("Failed to allocate memory for variable b");
            exit(1);
        }
        char *a=malloc(sizeof(char)*256);
        if(a==NULL)
        {
            puts("Failed to allocate memory for variable a");
            exit(1);
        }
    
        sIf = fopen("student.txt", "a+");
        if(!sIf)
        {
            printf("File could not be opened\n\a\a");
            return EXIT_FAILURE;
        }
        printf("Student First Name: \n");
        //Accept string using fgets, it prevents from overflow
        fgets(b,256,stdin);
        b[strlen(b)-1]='\0'; //Remove \n from the input string
        // Or you can use the following to remove `\n` from input string
        //if((ptr_pos=strchr(b, '\n')) != NULL)
        //  *ptr_pos = `\0`;
    
    
        printf("Student Last Name: \n");
        fgets(a,256,stdin);
        a[strlen(a)-1]='\0';
    
        printf("Student ID Number: \n");
        fgets(c,256,stdin);
        c[strlen(c)-1]='\0';
    
        fprintf(sIf, "%s, %s\t%s\n", a, b, c);       
    
        fclose(sIf);
        //Free allocated memory
        free(a);
        free(b);
        free(c);
    
        return EXIT_SUCCESS;
    }