Question

In: Computer Science

HW_6d - Write a struct object to a binary file. Create a new C++ project and...

HW_6d - Write a struct object to a binary file.

  1. Create a new C++ project and name it as:   Cats

  1. Create a Source.cpp file.

  1. Declare a struct named Cat. (in the Source.cpp file, or in a separate header file)

  1. Each Cat object has a name and age.
  • The name data member is a c_string.
  • The age data member is an integer.

  1. Ask the user to enter 3 cats.

  1. Use a while loop to read the information about one cat entered by the user, and write it to a

          binary file named: critters.bin

  • Only one Cat object needs to be declared, because the same object can be used for

     each cat.

  • Use the write() function to write the data to a binary file.
  • The while loop should only execute 3 iterations.

  1. Once the file has been written, the following message should be displayed:

      Record written to file.      (see output)

  1. Turn in a screen print of the binary file, as shown below, along with your code and output.

/* OUTPUT

Enter 3 cat records.

Enter information about a cat:

NAME: Tom

AGE: 5

Enter information about a cat:

NAME: Fluffy

AGE: 3

Enter information about a cat:

NAME: Sweet Pea

AGE: 2

Record written to file.

Press any key to continue . . . */

Please make the code based on C++, and make a description for each code.

Solutions

Expert Solution

C++ code:

#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;

//Defining the struct
struct Cat{
        string name;
        int age;
}c[3];                   //One structure object to contain 3 cats

int main(){
        
        ofstream wf("critters.bin", ios::out | ios::binary);  //opening the file in output mode
        
        if(!wf) {
      cout << "Cannot open file!" << endl;
      return 1;
        }
        
        cout<<"Enter 3 cat records."<<endl;
        
        //taking values from user
        int i = 0;
        while(i<3){                                  
                cout << "Enter information about a cat: "<< endl;
                cout << "NAME: ";
                cin >> c[i].name;
                cout << "AGE: ";
                cin >> c[i].age;
                wf.write((char *) &c[i], sizeof(Cat)); //writing struct object to binary file
                i++;
        }
        cout<<"Record written to file"<<endl;
        wf.close();
        return 0;
}

OUTPUT:

Binary file:


Related Solutions

Name your c++ file Word_LastNameFirstName.cpp. Create a struct that has a word and the length of...
Name your c++ file Word_LastNameFirstName.cpp. Create a struct that has a word and the length of the word. Next, use the struct to store the word read from a text file call “word.txt” and the length of the word. Print out the word x number of times based on the length of the word as shown below. Also, print a statement with the length and determine if the string length has an odd number or even number of characters. You...
Write a c program that creates a struct to be able to read a .img file...
Write a c program that creates a struct to be able to read a .img file and a .pat file.
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
This project requires the student to create a record (in C++ called a struct). The record...
This project requires the student to create a record (in C++ called a struct). The record should contain several fields of different data types and should allow the user to search the records to find the student with the highest grade. In this project the student should gain an understanding of the nature of records with multiple data types, how to save the records, search the records and retrieve them.  The special problems of maintaining records of multiple data types on...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers Create a text file and     save it as:   data.txt Create and save the file      in a C++ project      in the Resource folder. Enter the following numbers:        3                                                              4                                                              5       Note:   After you enter the 5, don’t press the enter key. Save and close the file. Add another file and name it:   Source.cpp Write one statement that declares a file...
write a Program in C++ Using a structure (struct) for a timeType, create a program to...
write a Program in C++ Using a structure (struct) for a timeType, create a program to read in 2 times into structures, and call the method addTime, in the format: t3 = addTime(t1, t2); Make sure to use add the code to reset and carry, when adding 2 times. Also, display the resultant time using a function: display(t3);
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
C++ Instantiate a binary search tree object and create such tree using elements of the sequence...
C++ Instantiate a binary search tree object and create such tree using elements of the sequence 8,3,10, 1,6,9, 14, 4,7, 13 with 8 as root of the tree. Find maximum and minimum elements of the tree, successor(10) and predecessor(13), print the inorder, postorder and preorder traversal of the tree.
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language...
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language A Game store sells many types of gaming consoles. The console brands are Xbox, Nintendo, PlayStation. A console can have either 16 or 8 gigabytes of memory. Use can choose the shipping method as either Regular (Cost it $5) or Expedite (Cost is $10) The price list is given as follows: Memory size/Brand Xbox Nintendo PlayStation 16 gigabytes 499.99 469.99 409.99 8 gigabytes 419.99...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT