分段错误发生在代码的非常直接的部分

发布于 2024-10-22 04:02:00 字数 2222 浏览 1 评论 0原文

当我第一次运行该程序时(尚未创建输出文件),它工作正常。但是当我再次运行它(输出文件存在)时,它在读取最后一个输入后给出分段错误。

从问题来看,它似乎与我的文件处理有关,但是当我尝试调试时,我发现我得到了 seg.在退出 main() 中的 for 循环之前发生错误。

#include <stdio.h>

#define EMPS 3

struct employee
{
    char firstname[40];
    char lastname[40];
    int id;
};
typedef struct employee Employee;

/* Input the employee data interactively from the keyboard */
void InputEmployeeRecord(Employee *ptrEmp);
/* Display the contents of a given Employee record */
void PrintEmployeeRecord(const Employee e);
/* Save the contents of the employee record list to the newly 
created text file specified by FileName */
void SaveEmployeeRecord(const Employee e[], const char *FileName);

int id = 0;

int main()
{
    Employee employees[EMPS];
    for(int i = 0; i < EMPS; i++)
    {
     InputEmployeeRecord(&(employees[i]));
     PrintEmployeeRecord(employees[i]);
    }
    SaveEmployeeRecord(employees, "employee.dat");
    return 0;
}

void InputEmployeeRecord(Employee *ptrEmp)
{
    printf("Enter first name\n");
    scanf("%s", ptrEmp->firstname);
    printf("Enter last name\n");
    scanf("%s", ptrEmp->lastname);
    ptrEmp->id = ++id;
}

void PrintEmployeeRecord(const Employee e)
{
    printf("Employee %d: %s %s\n", e.id, e.firstname, e.lastname);
}

void SaveEmployeeRecord(const Employee e[], const char *FileName)
{
    FILE* fptr = fopen(FileName, "r");

    /* File doesn't exist */
    if(fptr == NULL)
    {
        fclose(fptr);
        // Create the file
        FILE* fptr2 = fopen(FileName, "w");
        fclose(fptr2);
        // continue reading
        fptr = fopen(FileName, "r");
    }

    char firstLetter;
    int headerExists = 0;
    if(!feof( fptr ))
    {
        fscanf(fptr, "%c", firstLetter);
        if(firstLetter == 'I')
            headerExists = 1;
    }
    fclose(fptr);

    FILE* fptr2 = fopen(FileName, "a");
    if(!headerExists)
        fprintf(fptr2, "ID FIRSTNAME LASTNAME");
    for(int i = 0; i < EMPS; i++)
        fprintf(fptr2, "\n%d %s %s", e[i].id, e[i].firstname, e[i].lastname);
    fclose(fptr2);
}

[代码最初位于http://pastebin.com/tLefJhEH]

When I run the program the first time (the output file is not yet created), it works fine. But when I run it again (output file exists), it gives a segmentation fault after reading the last input.

From looking at the problem, it looks like it has something to do with my file handling, but when I tried to debug, I found out that I get the seg. fault before exiting the for loop in main().

#include <stdio.h>

#define EMPS 3

struct employee
{
    char firstname[40];
    char lastname[40];
    int id;
};
typedef struct employee Employee;

/* Input the employee data interactively from the keyboard */
void InputEmployeeRecord(Employee *ptrEmp);
/* Display the contents of a given Employee record */
void PrintEmployeeRecord(const Employee e);
/* Save the contents of the employee record list to the newly 
created text file specified by FileName */
void SaveEmployeeRecord(const Employee e[], const char *FileName);

int id = 0;

int main()
{
    Employee employees[EMPS];
    for(int i = 0; i < EMPS; i++)
    {
     InputEmployeeRecord(&(employees[i]));
     PrintEmployeeRecord(employees[i]);
    }
    SaveEmployeeRecord(employees, "employee.dat");
    return 0;
}

void InputEmployeeRecord(Employee *ptrEmp)
{
    printf("Enter first name\n");
    scanf("%s", ptrEmp->firstname);
    printf("Enter last name\n");
    scanf("%s", ptrEmp->lastname);
    ptrEmp->id = ++id;
}

void PrintEmployeeRecord(const Employee e)
{
    printf("Employee %d: %s %s\n", e.id, e.firstname, e.lastname);
}

void SaveEmployeeRecord(const Employee e[], const char *FileName)
{
    FILE* fptr = fopen(FileName, "r");

    /* File doesn't exist */
    if(fptr == NULL)
    {
        fclose(fptr);
        // Create the file
        FILE* fptr2 = fopen(FileName, "w");
        fclose(fptr2);
        // continue reading
        fptr = fopen(FileName, "r");
    }

    char firstLetter;
    int headerExists = 0;
    if(!feof( fptr ))
    {
        fscanf(fptr, "%c", firstLetter);
        if(firstLetter == 'I')
            headerExists = 1;
    }
    fclose(fptr);

    FILE* fptr2 = fopen(FileName, "a");
    if(!headerExists)
        fprintf(fptr2, "ID FIRSTNAME LASTNAME");
    for(int i = 0; i < EMPS; i++)
        fprintf(fptr2, "\n%d %s %s", e[i].id, e[i].firstname, e[i].lastname);
    fclose(fptr2);
}

[ Code originally at http://pastebin.com/tLefJhEH ]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

¢好甜 2024-10-29 04:02:00

问题可能出在 fscanf(fptr, "%c", firstLetter); 上。当您给它一个 char (在 c 中是一个整数)时,%c 期望一个 char* 作为输入。

要修复它,请尝试 fscanf(fptr, "%c", &firstLetter);

另外,不建议在空指针上调用 fclose。

The problem probably lies at the fscanf(fptr, "%c", firstLetter);. %c expects a char* as input while you are giving it a char (which is an integer in c).

To fix it try fscanf(fptr, "%c", &firstLetter);

Also calling fclose on a null pointer is not recommended.

猫腻 2024-10-29 04:02:00

我的错误,是这一行:

68.                fscanf(fptr, "%c", firstLetter);

应该是:

68.                fscanf(fptr, "%c", &firstLetter);

fscanfscanf 的参数是存储数据的内存位置的地址。

字符串不需要 &,因为字符串已经是一个地址。

My mistake, it's this line:

68.                fscanf(fptr, "%c", firstLetter);

Should be:

68.                fscanf(fptr, "%c", &firstLetter);

Argments to fscanf and scanf are addresses of the memory locations to store the data.

You don't need the & for a string because a string is already an address.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文