繁体   English   中英

Xcode上的C编程-EXC_BAD_ACCESS

[英]C Programming on Xcode - EXC_BAD_ACCESS

我目前正在编码一个C程序,以在文件中创建,查看和更新​​员工记录。 我的程序没有严重错误,一切顺利进行,直到我输入了员工ID,Xcode都会给出EXC_BAD_ACCESS错误。

我回到了我的代码,这发生在应该将数据写入文件的部分(请参阅注释),但是我仍然不确定这里出了什么问题。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size 300

struct emp
{
    int id;
    char *name;
}*emp1, *emp3;

void display();
void create();
void update();

FILE *fp, *fp1;
int count = 0;

void main(int argc, char **argv)
{
    int i, n, ch;

    printf("1] Create a Record\n");
    printf("2] Display Records\n");
    printf("3] Update Records\n");
    printf("4] Exit");
    while (1)
    {
        printf("\nEnter your choice : ");
        scanf("%d", &ch);
        switch (ch)
        {
            case 1:
                fp = fopen(argv[1], "a");
                create();
                break;
            case 2:
                fp1 = fopen(argv[1],"rb");
                display();
                break;
            case 3:
                fp1 = fopen(argv[1], "r+");
                update();
                break;
            case 4:
                exit(0);
        }
    }
}

/* To create an employee record */
void create()
{
    int i;
    char *p;

    emp1 = (struct emp *)malloc(sizeof(struct emp));
    emp1->name = (char *)malloc((size)*(sizeof(char)));
    printf("Enter name of employee : ");
    scanf(" %[^\n]s", emp1->name);
    printf("Enter emp id : ");
    scanf(" %d", &emp1->id);
    fwrite(&emp1->id, sizeof(emp1->id), 1, fp); //The error occurs here
    fwrite(emp1->name, size, 1, fp);
    count++;   // count to number of entries of records
    fclose(fp);
}

/* Display the records in the file */
void display()
{
    emp3=(struct emp *)malloc(1*sizeof(struct emp));
    emp3->name=(char *)malloc(size*sizeof(char));
    int i = 1;

    if (fp1 == NULL)
        printf("\nFile not opened for reading");
    while (i <= count)
    {
        fread(&emp3->id, sizeof(emp3->id), 1, fp1);
        fread(emp3->name, size, 1, fp1);
        printf("\n%d %s",emp3->id,emp3->name);
        i++;
    }
    fclose(fp1);
    free(emp3->name);
    free(emp3);
}

void update()
{
    int id, flag = 0, i = 1;
    char s[size];

    if (fp1 == NULL)
    {
        printf("File cant be opened");
        return;
    }
    printf("Enter employee id to update : ");
    scanf("%d", &id);
    emp3 = (struct emp *)malloc(1*sizeof(struct emp));
    emp3->name=(char *)malloc(size*sizeof(char));
    while(i<=count)
    {
        fread(&emp3->id, sizeof(emp3->id), 1, fp1);
        fread(emp3->name,size,1,fp1);
        if (id == emp3->id)
        {
            printf("Enter new name of emplyee to update : ");
            scanf(" %[^\n]s", s);
            fseek(fp1, -204L, SEEK_CUR);
            fwrite(&emp3->id, sizeof(emp3->id), 1, fp1);
            fwrite(s, size, 1, fp1);
            flag = 1;
            break;
        }
        i++;
    }
    if (flag != 1)
    {
        printf("No employee record found");
        flag = 0;
    }
    fclose(fp1);
    free(emp3->name);        /* to free allocated memory */
    free(emp3);
}

您在评论中提到fp为NULL。 这意味着您实际上没有要写入的打开文件。 较早的fopen调用返回NULL表示错误。 崩溃的直接原因是fwrite通过尝试访问应该指向的FILE结构的字段来取消引用空指针。 间接的原因是您在使用fopen之前没有检查它的返回值。

在每个fopen调用之后,添加如下内容:

if (fp == NULL)
{
  perror("Failed to open file");
  exit(1);
}

fopen失败时,它将返回NULL并设置全局变量errno来指示出了什么问题。 perror函数打印与errno值相对应的描述性消息。


因为你的程序需要被赋予在命令行上的文件名(即argv[1]确保Xcode中实际上是给它一个。启动程序从IDE通常运行它没有默认情况下任何命令行参数。访问argv[1]argc小于2时,将是另一个错误,并且可能会显示为不是有效文件名的垃圾字符串,从而导致fopen失败。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM