Da li neko moze da objasni kako funkcija fwrite() smesta podatke u fajl ako mu se kao prvi agrument da adresa strukture (struct). Evo ako neko moze na sledecem prostom primeru da objasni po kojoj logici previ fajl "housfile" odnosno, po kojoj logici ja mogu da ga citam (za hex viewer-om)
Sledeci primer je kompajliran i u DOS (Trubo C) iu Linuxu (GCC), fhread.c:
#include <stdio.h>
#include <string.h>
#define YES 1
#define NO 0
main()
{
FILE *f;
struct proba
{
unsigned int gas : 1; /* 1 bit */
unsigned int air : 1; /* 1 bit */
unsigned int dish : 1; /* 1 bit */
unsigned int wash : 1; /* 1 bit */
unsigned int block : 8; /* 8 bita */
unsigned char lot[6];
}input;
if ((f=fopen("housfile","w"))==(FILE *)NULL)
{
printf("Cannot create file "housfile"n");
exit(1);
}
input.gas=YES;
input.air=YES;
input.dish=NO;
input.wash=YES;
input.block=23;
strcpy(input.lot,"5a");
fwrite(&input,sizeof (struct proba),1,f);
fclose(f);
if ((f=fopen("housfile","r"))==(FILE *)NULL)
{
printf("Cannot open "housfile"n");
exit(1);
}
while (fread(&input,sizeof (struct proba),1,f)!=0)
{
printf("BLOCK: %d LOT: %sn",input.block,input.lot);
printf("GAS HEAT: %s CENTRAL AIR: %sn",input.gas==YES ? "YES" : "NO",
input.air==YES ? "YES" : "NO");
printf("DISHWASHER: %s WASHER-DRYER: %sn",input.dish==YES ? "YES" : "NO",
input.wash==YES ? "YES" : "NO");
puts("***********");
}
fclose(f);
}
/*
program output:
BLOCK: 23 LOT: 5a
GAS HEAT: YES CENTRAL AIR: YES
DISHWASHER: NO WASHER-DRYER: YES
***********
Fajl "hausfile" izgleda ovako:
"\x7b\xf1\x35\x61\x00\xd9\x00\x40" (u Linux-u)
"x7b\x01\x35\x61\x00\x01\x00\x00" (u DOS-u)
ukupno 8 bajta koliko je i velicina struct proba (sizeof (struct proba)).
*/
Sledeci primer je kompajliran i u DOS (Trubo C) iu Linuxu (GCC), fhread.c:
#include <stdio.h>
#include <string.h>
#define YES 1
#define NO 0
main()
{
FILE *f;
struct proba
{
unsigned int gas : 1; /* 1 bit */
unsigned int air : 1; /* 1 bit */
unsigned int dish : 1; /* 1 bit */
unsigned int wash : 1; /* 1 bit */
unsigned int block : 8; /* 8 bita */
unsigned char lot[6];
}input;
if ((f=fopen("housfile","w"))==(FILE *)NULL)
{
printf("Cannot create file "housfile"n");
exit(1);
}
input.gas=YES;
input.air=YES;
input.dish=NO;
input.wash=YES;
input.block=23;
strcpy(input.lot,"5a");
fwrite(&input,sizeof (struct proba),1,f);
fclose(f);
if ((f=fopen("housfile","r"))==(FILE *)NULL)
{
printf("Cannot open "housfile"n");
exit(1);
}
while (fread(&input,sizeof (struct proba),1,f)!=0)
{
printf("BLOCK: %d LOT: %sn",input.block,input.lot);
printf("GAS HEAT: %s CENTRAL AIR: %sn",input.gas==YES ? "YES" : "NO",
input.air==YES ? "YES" : "NO");
printf("DISHWASHER: %s WASHER-DRYER: %sn",input.dish==YES ? "YES" : "NO",
input.wash==YES ? "YES" : "NO");
puts("***********");
}
fclose(f);
}
/*
program output:
BLOCK: 23 LOT: 5a
GAS HEAT: YES CENTRAL AIR: YES
DISHWASHER: NO WASHER-DRYER: YES
***********
Fajl "hausfile" izgleda ovako:
"\x7b\xf1\x35\x61\x00\xd9\x00\x40" (u Linux-u)
"x7b\x01\x35\x61\x00\x01\x00\x00" (u DOS-u)
ukupno 8 bajta koliko je i velicina struct proba (sizeof (struct proba)).
*/