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 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.
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);
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 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.
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...
Create a List object that uses the binary search algorithm to search for the string "A"....
Create a List object that uses the binary search algorithm to search for the string "A". Display a message box indicating whether the value was found. Language: C#
Create a C++ class with a static member item so that whenever a new object is...
Create a C++ class with a static member item so that whenever a new object is created, the total number of objects of the class can be reported.
In this assignment you will build a small C# project that uses… • A struct •...
In this assignment you will build a small C# project that uses… • A struct • A method with a reference parameter • A while-loop • A switch statement and block instructions: Inside the StringHandler struct add a public void method named Abbreviate. The Abbreviate method must take a string parameter that is passed by reference. This method will take the name of a month (January, February, etc.) as its input and convert it to a 3-letter abbreviation of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT