2012-11-23 99 views
2

如何將值傳遞給結構變量我試圖從用戶獲取員工信息,然後將其寫入文件中,但輸入員工姓名後我得到segmentation fault。 這是我的代碼。如何將值傳遞給結構變量,然後將結構寫入文件?

#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; 
    } 

輸出(從評論拍攝):

 
Fatmahs-MacBook-Air:~ fatmah$ gcc -o em em.c 
Fatmahs-MacBook-Air:~ fatmah$ ./em 
Enter Employee ID 
88 
Enter Employee Name 
uu 
Segmentation fault: 11 
+2

我不明白,因爲這不應該請編譯如何發生分段錯誤:'employee.name =名稱;'。這是_exact_代碼嗎? – hmjd

+0

是,'Fatmahs-的MacBook空中:〜fatmah $ gcc的-o EM em.c Fatmahs-的MacBook空中:〜fatmah $ ./em 輸入員工ID 輸入員工姓名 UU 分段故障:11' – fatimah

+0

請參閱http://ideone.com/3JvSwG for _error:assignment_編譯器失敗消息中的不兼容類型。 – hmjd

回答

6

變化

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

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

中,更好的是,由Dukeling &建議hmjd

scanf("%19s", employee.name); 
+2

剛剛'scanf(「%s」,employee.name)''怎麼樣?小心緩衝區溢出。 – Dukeling

+0

@Dukeling好點,那會更好 – simonc

+1

再好一點:'scanf(「%19s」,employee.name);'以避免緩衝區溢出。 – hmjd

3

這是一個大問題:

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

成員name陣列,你不能給它分配。請使用strcpy複製即可。

+0

謝謝它的作品,但它寫了一個垃圾文件! – fatimah

0
  1. 創建 typedef結構record_t使事情變得更短,更容易理解。

    typedef struct { 
        int id; 
        char name[20]; 
        int salary; 
        int age; 
    } record_t; 
    
  2. 創建文件,並首先進行格式化。

    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. 
    } 
    
  3. 編寫函數來填補文件。

    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. 
    } 
    

您可以使用此作爲參考Comparing and checking columns in two files

+0

非常感謝,但它並沒有寫入整數! – fatimah