Question

Goals: Write a program that uses binary files. Write a program that stores records to a...

Goals:

  • Write a program that uses binary files.
  • Write a program that stores records to a binary file.
  • C++

Requirements:

Write a program that includes a structure named Part that contains the following fields:

  • name - a character array of 20 elements that stores the name of the part.
  • qty - an integer that stores the number of parts in stock.
  • price - a double that stores the price of the part.

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:

  1. Attempt to open the file.
  2. Verify the file opened.
  3. If the file opened, process it and close it.
  4. If the file didn't open, print an appropriate error message.

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.

Homework Answers

Answer #1
#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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
C# Reading from Files Write a program to open a text file containing information about buildings....
C# Reading from Files Write a program to open a text file containing information about buildings. The program must display all the buildings in the file and then find the lowest cost building based on the cost per square foot. Format for one building: <building name> <size> sqft $<cost> Example: Allgood Hall 35000 sqft $8,250,099.75 Create the text file and add three or more of your favorite buildings.
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers....
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers. The data for this program will be entered from the keyboard using JOptionPane one 16-bit binary number at a time. Note that each base 2 number is actually read in as a String. The program should continue until a 16-bit base 2 number consisting of all 0’s is entered. Once the 16-bit number has been entered your program should make sure that the input...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain account numbers (10 digits) along with the account type (L:Loan and S:Savings) and their current balance. The second file (transactions.txt) will contain transactions on the accounts. It will specifically include the account number, an indication if it is a withdrawal/deposit and an amount. Both files will be pipe delimited (|) and their format will be the following: accounts.txt : File with bank account info...
In the book_store.cpp file, add the code for the process_orders method. The argument that is passed...
In the book_store.cpp file, add the code for the process_orders method. The argument that is passed to this method is the name of a file that will be read in the method. Each line in the file contains an order number (integer), isbn number (character array), and amount ordered (integer). If an order number is on a line, it is guaranteed that there will be an isbn number and amount ordered to go along with it. Create an input file...
Part 1 Write a program where a child is created to execute command that tells you...
Part 1 Write a program where a child is created to execute command that tells you the date and time in Unix. Use ​execl(...)​. Note: you need to specify the full path of the file name that gives you date and time information. Announce the successful forking of child process by displaying its PID. Part 2 Write a program where a child is created to execute a command that shows all files (including hidden files) in a directory with information...
This program is in C++: Write a program to allow the user to: 1. Create two...
This program is in C++: Write a program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods. 2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to...
C++ Programming   You are to develop a program to read Baseball player statistics from an input...
C++ Programming   You are to develop a program to read Baseball player statistics from an input file. Each player should bestored in a Player object. Therefore, you need to define the Player class. Each player will have a firstname, last name, a position (strings) and a batting average (floating point number). Your class needs to provide all the methods needed to read, write, initialize the object. Your data needs to be stored in an array of player objects. The maximum...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...