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.
Write a C program that creates a structure and displays its content. • Create a struct...
Write a C program that creates a structure and displays its content. • Create a struct that will be used to hold a student's name, age, and year in school (Freshman, Sophomore, Junior, or Senior) • Within function main, dynamically allocate space to hold the structure and assign a pointer to point to the memory space allocated • Read in (from the keyboard) the student's name, age, and year in school • Create a separate function with the prototype: void...
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 new project to build a Binary search tree, and do the following: Create a...
Create a new project to build a Binary search tree, and do the following: Create a TreeNode class, Add the methods "Insert" to insert new nodes to the tree. Add the method "inorder". Make the method to return a list of the node elements in inorder. Implement the equals method in the BST. Two BST are equal if they contain the same elements. In the main insert the elements of the tree, call the max method and print the max...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT