Goals:
Requirements:
Write a program that includes a structure named Part that contains the following fields:
Create three Part variables in main, and fill them with data of your choice (a name, a quantity in stock, and a price). Then, write each of these variables to a binary file named records.dat. Make sure you follow the four standard steps for dealing with files:
A Sample Run:
The only output you could see would be the error message. However, check to make sure the file was written to by using Windows explorer and checking the size of the file. If the records were successfully written, it will not be 0 bytes long.
#include <stdio.h>
#include <stdlib.h>
struct Part
{
char name[20];
int qty;
double price;
};
int main()
{
struct Part p1={"nut",20,4.5},p2={"brakes",30,2.32},p3={"bolts",10,5.0};
FILE *fptr;
fptr=fopen("records.dat","wb");
if(fptr==NULL)
{
printf("not able to open file");
return 0;
}
fwrite(&p1, sizeof(struct Part), 1, fptr);
fwrite(&p2, sizeof(struct Part), 1, fptr);
fwrite(&p3, sizeof(struct Part), 1, fptr);
fclose(fptr);
return 0;
}
This program runs correctly and writes the values of p1,p2 and p3 into the file records.dat.
Get Answers For Free
Most questions answered within 1 hours.