How would I take this code and write it into a .dat file using fwrite? This is i
ID: 3913293 • Letter: H
Question
How would I take this code and write it into a .dat file using fwrite? This is in C
#include
#include
#include
int main()
{
dog apollo;
strcpy( apollo.color, "brown" );
strcpy( apollo.ageInHumanYears, "28" );
strcpy( apollo.height, "1.5");
strcpy( apollo.texture, "rough" );
dog athena;
strcpy( athena.color, "golden" );
strcpy( athena.ageInHumanYears, "35");
strcpy( athena.height, "1.8");
strcpy( athena.texture, "silky" );
dog hercules;
strcpy( hercules.color, "white" );
strcpy( hercules.ageInHumanYears, "14");
strcpy( hercules.height, "2");
strcpy( hercules.texture, "furry" );
dog zeus;
strcpy( zeus.color, "black" );
strcpy( zeus.ageInHumanYears, "42");
strcpy( zeus.height, "1.4");
strcpy( zeus.texture, "fluffy" );
dog poseidon;
strcpy( poseidon.color, "gray" );
strcpy( poseidon.ageInHumanYears, "56");
strcpy( poseidon.height, "2.1");
strcpy( poseidon.texture, "dense" );
return 0;
}
Explanation / Answer
#include<stdio.h>
int main ()
{
// open file in write mode
FILE *fp = fopen( "output.dat" , "w" );
dog apollo;
strcpy( apollo.color, "brown" );
// write into file
fwrite(apollo.color , 1 , sizeof(apollo.color) , fp );
strcpy( apollo.ageInHumanYears, "28" );
// write into file
fwrite(apollo.ageInHumanYears , 1 , sizeof(apollo.ageInHumanYears) , fp );
strcpy( apollo.height, "1.5");
// write into file
fwrite(apollo.height , 1 , sizeof(apollo.height) , fp );
strcpy( apollo.texture, "rough" );
// write into file
fwrite(apollo.texture , 1 , sizeof(apollo.texture) , fp );
dog athena;
strcpy( athena.color, "golden" );
// write into file
fwrite(athena.color , 1 , sizeof(athena.color) , fp );
strcpy( athena.ageInHumanYears, "35");
// write into file
fwrite(athena.ageInHumanYears , 1 , sizeof(athena.ageInHumanYears) , fp );
strcpy( athena.height, "1.8");
// write into file
fwrite(athena.height , 1 , sizeof(athena.height) , fp );
strcpy( athena.texture, "silky" );
// write into file
fwrite(athena.texture , 1 , sizeof(athena.texture) , fp );
dog hercules;
strcpy( hercules.color, "white" );
// write into file
fwrite(hercules.color , 1 , sizeof(hercules.color) , fp );
strcpy( hercules.ageInHumanYears, "14");
// write into file
fwrite(hercules.ageInHumanYears , 1 , sizeof(hercules.ageInHumanYears) , fp );
strcpy( hercules.height, "2");
// write into file
fwrite(hercules.height , 1 , sizeof(hercules.height) , fp );
strcpy( hercules.texture, "furry" );
// write into file
fwrite(hercules.texture , 1 , sizeof(hercules.texture) , fp );
dog zeus;
strcpy( zeus.color, "black" );
// write into file
fwrite(zeus.color , 1 , sizeof(zeus.color) , fp );
strcpy( zeus.ageInHumanYears, "42");
// write into file
fwrite(zeus.ageInHumanYears , 1 , sizeof(zeus.ageInHumanYears) , fp );
strcpy( zeus.height, "1.4");
// write into file
fwrite(zeus.height , 1 , sizeof(zeus.height) , fp );
strcpy( zeus.texture, "fluffy" );
// write into file
fwrite(zeus.texture , 1 , sizeof(zeus.texture) , fp );
dog poseidon;
strcpy( poseidon.color, "gray" );
// write into file
fwrite(poseidon.color , 1 , sizeof(poseidon.color) , fp );
strcpy( poseidon.ageInHumanYears, "56");
// write into file
fwrite(poseidon.ageInHumanYears , 1 , sizeof(poseidon.ageInHumanYears) , fp );
strcpy( poseidon.height, "2.1");
// write into file
fwrite(poseidon.height , 1 , sizeof(poseidon.height) , fp );
strcpy( poseidon.texture, "dense" );
// write into file
fwrite(poseidon.texture , 1 , sizeof(poseidon.texture) , fp );
// close the file
fclose(fp);
return(0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.