如何在C中的这个二进制文件中定位以读取数据?

问题描述

我不知道如何在这个二进制文件中定位来读取工资金额。英语是我的第二语言,我是在学校学习编程的初学者,所以请理解我。

/* 1. 编写一个程序,要求用户在二进制文件中输入数据:他的姓名、出生日期、他几岁、他的工资是多少、他的地址是什么以及他的是他的电话号码。然后关闭创建的二进制文件,然后打开它只是为了读取和读取用户的工资,然后将其写在屏幕上。 */

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

typedef struct

{
    int d;
    int m;
    int g; //year
}Datum; //date

void main()
{
    FILE *korisnici=NULL;
    char ime[20],prezime[20],adresa[50],telefon[20];
    int godine; 
    float plata,pl=0;
    Datum rodjendan;

    korisnici=fopen("C:\\Users\\x\\Documents\\II godina\\programski jezici\\priprema za lab\\korisnici.bin","w+b");

    if(korisnici == NULL)
        printf("greska."); //"error"
    else
        printf("fajl uspjesno otvoren.\n"); //"file opened successfully"

    printf("ime: "); //name
    gets(ime);
    fwrite(ime,sizeof(char),strlen(ime),korisnici);

    printf("prezime: "); //surname
    gets(prezime);
    fwrite(prezime,strlen(prezime),korisnici);

    printf("datum rodjenja: "); //date of birth
    scanf("%d-%d-%d: ",&rodjendan.d,&rodjendan.m,&rodjendan.g);
    fwrite(&rodjendan,sizeof(Datum),1,korisnici);

    printf("godine: "); //years
    scanf("%d",&godine);
    fwrite(&godine,sizeof(int),korisnici);

    printf("iznos plate: "); //salary
    scanf("%f",&plata);
    fwrite(&plata,sizeof(float),korisnici);

    printf("adresa: "); //address
    gets(adresa);   //I intentionally repeated this command
    gets(adresa);
    fwrite(adresa,strlen(adresa),korisnici);

    printf("broj telefona: "); //phone number
    scanf("%s",telefon);
    fwrite(telefon,strlen(telefon),korisnici);

    printf("podaci su upisani u fajl korisnici.bin.\n"); //"the data is written to the file korisnici.bin"
    fclose(korisnici);

    system("cls");
    korisnici=fopen("C:\\Users\\x\\Documents\\II godina\\programski jezici\\priprema za lab\\korisnici.bin","r+b");
    
    if(korisnici == NULL)
        printf("greska.");
    else
        printf("fajl uspjesno otvoren.\n");

    printf("korisnikova plata iznosi: "); //"the user's salary is: "

    //fseek needed here

    fread(&pl,korisnici);
    printf("%.2f BAM.\n",pl);
    fclose(korisnici);
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)