如何将值传递给struct变量,然后将struct写入文件?

如何将值传递给struct变量,然后将struct写入文件?,c,file,struct,segmentation-fault,fwrite,C,File,Struct,Segmentation Fault,Fwrite,如何将值传递给struct变量我正试图从用户那里获取员工信息,然后将其写入文件,但在输入员工姓名后,出现了分段错误。 这是我的密码 #include <stdio.h> #include <string.h> #include <stdlib.h> struct record_em{ int id; char name[20]; int salary; int age; }; int main( void ) { s

如何将值传递给struct变量我正试图从用户那里获取员工信息,然后将其写入文件,但在输入员工姓名后,出现了
分段错误。
这是我的密码

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

struct record_em{
    int id;
    char name[20];
    int salary;
    int age;
};

int main( void )
{
    struct record_em employee;
    FILE *fp;
    int id, salary, age;
    char name[20];
    int n=1;

    fp = fopen("empRecord.dat","a");
    while(n==1){
        printf("\nEnter Employee ID\n");
        scanf("%d",&id);
        employee.id=id;
        printf("\nEnter Employee Name\n");
        scanf("%s",name);
        employee.name=name;
        printf("\nEnter Employee Salary\n");
        scanf("%d",&salary);
        employee.salary=salary;
        printf("\nEnter Employee Age\n");
        scanf("%d",&age);
        employee.age=age;
        fwrite(&employee,sizeof(employee),1,fp);
        printf("Enter 1 to add new record \n");
        scanf("%d",&n);
    }

    fclose(fp);

    return 0;
    }
#包括
#包括
#包括
结构记录{
int-id;
字符名[20];
国际工资;
智力年龄;
};
内部主(空)
{
结构记录\u em员工;
文件*fp;
int id、工资、年龄;
字符名[20];
int n=1;
fp=fopen(“empRecord.dat”,“a”);
而(n==1){
printf(“\n输入员工ID\n”);
scanf(“%d”和&id);
employee.id=id;
printf(“\n输入员工姓名\n”);
scanf(“%s”,名称);
employee.name=姓名;
printf(“\n输入员工工资”);
scanf(“%d”和工资);
员工工资=工资;
printf(“\n输入员工年龄\n”);
scanf(“%d”和年龄);
雇员。年龄=年龄;
fwrite(和employee,sizeof(employee),1,fp);
printf(“输入1以添加新记录\n”);
scanf(“%d”和“&n”);
}
fclose(fp);
返回0;
}
输出(摘自注释):

Fatmahs MacBook Air:~fatmah$gcc-o em.c Fatmahs MacBook Air:~fatmah$/em 输入员工ID 88 输入员工姓名 uu 分段错误:11
这里有一个主要问题:

scanf("%s",name);
employee.name=name;
成员
名称
是一个数组,不能分配给它。改为使用复制到它。

更改

scanf("%s",name);
employee.name=name;

当然,更好的是,正如Dukeling&hmjd所建议的那样

scanf("%19s", employee.name);
  • 创建一个typedef结构
    记录
    ,使内容更短、更容易理解

    typedef struct {
        int id;
        char name[20];
        int salary;
        int age;
    } record_t;
    
  • 首先创建文件并格式化

    void file2Creator( FILE *fp )
    {
        int i; // Counter to create the file.
        record_t data = { 0, "", 0, 0 }; // A blank example to format the file.
    
        /* You will create 100 consecutive records*/
        for( i = 1; i <= 100; i++ ){
            fwrite( &data, sizeof( record_t ), 1, fp );
        }
    
        fclose( fp ); // You can close the file here or later however you need.  
    }
    

  • 您可以将其用作参考

    仅使用
    scanf(“%s”,employee.name)”如何
    ?小心缓冲区溢出。最好是:
    scanf(“%19s”,employee.name)以避免缓冲区溢出。写入文件的内容是什么?您是使用文本编辑器还是十六进制编辑器读取文件?请记住,您写入的整数将被写入4字节,而不会在文本编辑器中呈现为数字。名称将包含一些文本,然后是nul(字符代码0)字符,然后是一些未初始化的数据。左侧的十六进制编辑器显示十六进制数,右侧仅显示名称1@fatimah您已经创建了一个简单形式的二进制文件。其中的整数将表示为4个字节(字符);这些字节通常没有映射到文本编辑器中可打印字符的值。您只能在十六进制编辑器中明智地查看内容。如果您不了解文件中数据的形式,验证它的另一种方法是编写一个小程序,执行类似于
    fread的操作(&employee,sizeof(employee),1,fp)
    然后打印出“employee”的字段。我不明白分段错误是如何发生的,因为这不应该包括:
    employee.name=name。这是准确的代码吗?是的,
    Fatmahs MacBook Air:~fatmah$gcc-o em em.c Fatmahs MacBook Air:~fatmah$。/em输入员工ID 88输入员工姓名uu分段错误:11
    请参阅错误:分配编译器失败消息中的不兼容类型。稍微看一下[比较和检查两个文件中的列][1]:[1]:
    typedef struct {
        int id;
        char name[20];
        int salary;
        int age;
    } record_t;
    
    void file2Creator( FILE *fp )
    {
        int i; // Counter to create the file.
        record_t data = { 0, "", 0, 0 }; // A blank example to format the file.
    
        /* You will create 100 consecutive records*/
        for( i = 1; i <= 100; i++ ){
            fwrite( &data, sizeof( record_t ), 1, fp );
        }
    
        fclose( fp ); // You can close the file here or later however you need.  
    }
    
    void fillFile( FILE *fp )
    {
        int position;
        record_t data = { 0, "", 0, 0 };
    
    
        printf( "Enter the position to fill (1-100) 0 to finish:\n?" );
        scanf( "%d", &position );
    
        while( position != 0 ){
            printf( "Enter the id, name, and the two other values (integers):\n?" );
            fscanf( stdin, "%d%s%d%d", &data.id, data.name, data.salary, data.age );
    
            /* You have to seek the pointer. */
            fseek( fp, ( position - 1 ) * sizeof( record_t ), SEEK_SET );
            fwrite( &data, sizeof( record_t ), 1, fp );
            printf( "Enter a new position (1-100) 0 to finish:\n?" );
            scanf( "%d", &position );
        }
    
        fclose( fPtr ); //You can close the file or not, depends in what you need.
    }