Hello, whenever I run this program my .exe crashes and leaves the resulting .dat
ID: 3913498 • Letter: H
Question
Hello, whenever I run this program my .exe crashes and leaves the resulting .dat file empty, this is done in C
#include
#include
#include
#include
int main ()
{
FILE *fp = fopen( "dogs.dat" , "wb" );
Dogs apollo;
strcpy(apollo.colors, "brown");
apollo.ageInHumanYears = "28";
apollo.height = "1.5";
strcpy(apollo.texture, "rough");
fwrite(&apollo,sizeof (Dogs),1,fp);
Dogs athena;
strcpy(athena.colors, "golden");
athena.ageInHumanYears = "35";
athena.height = "1.8";
strcpy(athena.texture, "silky");
fwrite(&athena,sizeof (Dogs),1,fp);
Dogs hercules;
strcpy(hercules.colors, "white");
hercules.ageInHumanYears = "14";
hercules.height = "2";
strcpy(hercules.texture, "furry");
fwrite(&hercules,sizeof (Dogs),1,fp);
Dogs zeus;
strcpy(zeus.colors, "black");
zeus.ageInHumanYears = "42";
zeus.height = "1.4";
strcpy(zeus.texture, "fluffy");
fwrite(&zeus,sizeof (Dogs),1,fp);
Dogs poseidon;
strcpy(poseidon.colors, "gray");
poseidon.ageInHumanYears = "56";
poseidon.height = "2.1";
strcpy(poseidon.texture, "dense");
fwrite(&poseidon,sizeof (Dogs),1,fp);
fclose(fp);
return(0);
}
This is the code for the dogs struct.h file
#ifndef DOGS_H
#define DOGS_H
typedef struct Dogs_struct
{
char name[20]; // Preferably the dog's full name
char colors[30]; // List the color(s) of the dog's fur (or skin if applicable)
int ageInHumanYears;
int height; // Let the number before the decimal equal feet and the number after the decimal equal inches
char texture; // how does their fur feel like: rough, smooth, thick, soft, and son on
} Dogs;
#endif // DOGS_H
Explanation / Answer
/*
Modified c program that open an input
file dogs.dat in write mode for writing
data in binary form.
*/
//include header files
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string>
//include Dogs.h header file
#include "Dogs.h"
int main ()
{
//Open file for writing in binary mode
FILE *fp = fopen( "dogs.dat" , "wb" );
//Create variable of Dogs strcture
Dogs apollo;
strcpy(apollo.colors, "brown");
//Story only the integer type for ageInHumanuYears
apollo.ageInHumanYears = 28;
apollo.height = 1.5;
strcpy(apollo.texture, "rough");
fwrite(&apollo,sizeof (Dogs),1,fp);
//Create variable of Dogs strcture
Dogs athena;
strcpy(athena.colors, "golden");
athena.ageInHumanYears = 35;
athena.height = 1.8;
strcpy(athena.texture, "silky");
fwrite(&athena,sizeof (Dogs),1,fp);
//Create variable of Dogs strcture
Dogs hercules;
strcpy(hercules.colors, "white");
hercules.ageInHumanYears = 14;
hercules.height = 2;
strcpy(hercules.texture, "furry");
fwrite(&hercules,sizeof (Dogs),1,fp);
//Create variable of Dogs strcture
Dogs zeus;
strcpy(zeus.colors, "black");
zeus.ageInHumanYears = 42;
zeus.height = 1.4;
strcpy(zeus.texture, "fluffy");
fwrite(&zeus,sizeof (Dogs),1,fp);
//Create variable of Dogs strcture
Dogs poseidon;
strcpy(poseidon.colors, "gray");
poseidon.ageInHumanYears = 56;
poseidon.height = 2.1;
strcpy(poseidon.texture, "dense");
fwrite(&poseidon,sizeof (Dogs),1,fp);
//Close the file pointer
fclose(fp);
getch();
return(0);
}
-------------------------------------------------------------
//Dogs.h
#ifndef DOGS_H
#define DOGS_H
typedef struct Dogs_struct
{
char name[20];
char colors[30];
int ageInHumanYears;
int height;
//declare charaacter array to stroe the texture
char texture[20];
} Dogs;
#endif // DOGS_H
--------------------------------------------------------
Note : The data written to file dogs.txt and it is not able to read in text form since it is written in binary.
To read the file, has to write a code to read file in binary into the text format.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.