开发者

Segmentation fault occurring at very straight-forward part of the code

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 ]


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜