Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
109 views
in Technique[技术] by (71.8m points)

how to position in this binary file in C to read data?

I don't know how to position in this binary file to read the amount of salary. English is my second language and I am beginner in learning programming in school so please have understanding for me.

/* 1. Write a program that asks the user to enter data in a binary file: his name and surname, date of birth, how old is he, what is the amount of his salary, what is his address and what is his phone number. Then close the created binary file and then open it just to read and read what is the user's salary then write it on screen. */

#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.
"); //"file opened successfully"

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

    printf("prezime: "); //surname
    gets(prezime);
    fwrite(prezime, sizeof(char), 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), 1, korisnici);

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

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

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

    printf("podaci su upisani u fajl korisnici.bin.
"); //"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.
");

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

    //fseek needed here

    fread(&pl, sizeof(float), 1, korisnici);
    printf("%.2f BAM.
", pl);
    fclose(korisnici);
}
question from:https://stackoverflow.com/questions/65863954/how-to-position-in-this-binary-file-in-c-to-read-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...