Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Reading binary data Reading data in binary is all about copying bytes (1 byte:- 2 nibbles:8 bits) from a location in a file to a

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Reading binary data Reading data in binary is all about copying bytes (1 byte:- 2 nibbles:8 bits) from a location in a file to a location in memory. When reading data you will always use the read() function, and when writing data you will always use the write() function. For this assignment, you will only need to read() data Strings are always an exceptional case. In the case of strings, you should read them in a 4 or 5 step process Read the length of the string from the file. Unless you are dealing with fixed-length strings (in which case you know the length of the string from somewhere else), it will be there, promise. (If someone didn't write this data out to a file, shame on them, they screwed up.) Dynamically allocate an array equal to the size of the string, plus 1 for the null terminator. If the length already includes the null terminator, do not add one to the count here - you'd be accounting for it twice, which is bad 1. 2. 3. Read the string into your newly created buffer. 4. (OPTIONAL) Store your dynamic char * in something like a std::string, which manages its own internal memory Then you don't have to worry about it anymore 5. Delete the dynamically allocated array. If you did step 4, this should be immediately after you store it in the std::string (so you don't forget to delete it later). If you are planning to use this variable later, be sure to delete it later on down the line Refer back to the Powerpoint slides about Binary File I/O for information on how to read and write binary files File format The structure of the files is as follows: 4 bytes A count of how many ships are in the file "Count" number of ships follow the first 4 bytes. Each ship has the following format: 4 bytes "Length" bytes 4 bytes "Length" bytes 2 bytes 4 bytes 4 bytes 4 bytes The length of the name, including the null terminator The string data for the name, including the null terminator The length of the ship's class, including the null terminator The string data for the class, including the null terminator The ship's length, in meters The ship's shield capacity, in terajoules The warp speed of the ship A count of the number of weapons equipped on the ship "Count" number of weapons follow the previous 4 bytes. Each Item is as follows: 4 bytes "Length" bytes 4 bytes The length of the weapon's name, including the null terminator The string data for the name of the item, including the null terminator An integer for power rating of the weapon (on some fictional scale-higher is better) A float for the power consumption of the weapon 4 bytes Tips Choices you make at the start of a program can have a big impact on how the rest of the program gets developed. Think about how you want to store the information retrieved from the file, and how you could easily pass that data to various functions you might write. If you have a process for easily loading and accessing the data, the rest of the functionality should be a lot easier to write. Make sure the loading process is all taken care of before worrying about anything else. 1. 2. 3. The code to load 1 file containing 1 piece of data (no matter how complex that data is) should not be much different than loading 100 files, each containing 100 elements. Start by thinking about just 1 element from the file first. Do the values you read match the values in the file? What about 2 entries, does everything add up? Etc... 4. If you pass containers of data, make sure you pass them by REFERENCE, not by value. Don't create copies of anything unless you specifically need a copy. Try reading one element at a time. Read the first 4 bytes, try printing it out to the screen. Is the number something reasonable, or something that seems incorrect, like -20? If that works, move on to the next piece of data in the file. 5. Reading binary data Reading data in binary is all about copying bytes (1 byte:- 2 nibbles:8 bits) from a location in a file to a location in memory. When reading data you will always use the read() function, and when writing data you will always use the write() function. For this assignment, you will only need to read() data Strings are always an exceptional case. In the case of strings, you should read them in a 4 or 5 step process Read the length of the string from the file. Unless you are dealing with fixed-length strings (in which case you know the length of the string from somewhere else), it will be there, promise. (If someone didn't write this data out to a file, shame on them, they screwed up.) Dynamically allocate an array equal to the size of the string, plus 1 for the null terminator. If the length already includes the null terminator, do not add one to the count here - you'd be accounting for it twice, which is bad 1. 2. 3. Read the string into your newly created buffer. 4. (OPTIONAL) Store your dynamic char * in something like a std::string, which manages its own internal memory Then you don't have to worry about it anymore 5. Delete the dynamically allocated array. If you did step 4, this should be immediately after you store it in the std::string (so you don't forget to delete it later). If you are planning to use this variable later, be sure to delete it later on down the line Refer back to the Powerpoint slides about Binary File I/O for information on how to read and write binary files File format The structure of the files is as follows: 4 bytes A count of how many ships are in the file "Count" number of ships follow the first 4 bytes. Each ship has the following format: 4 bytes "Length" bytes 4 bytes "Length" bytes 2 bytes 4 bytes 4 bytes 4 bytes The length of the name, including the null terminator The string data for the name, including the null terminator The length of the ship's class, including the null terminator The string data for the class, including the null terminator The ship's length, in meters The ship's shield capacity, in terajoules The warp speed of the ship A count of the number of weapons equipped on the ship "Count" number of weapons follow the previous 4 bytes. Each Item is as follows: 4 bytes "Length" bytes 4 bytes The length of the weapon's name, including the null terminator The string data for the name of the item, including the null terminator An integer for power rating of the weapon (on some fictional scale-higher is better) A float for the power consumption of the weapon 4 bytes Tips Choices you make at the start of a program can have a big impact on how the rest of the program gets developed. Think about how you want to store the information retrieved from the file, and how you could easily pass that data to various functions you might write. If you have a process for easily loading and accessing the data, the rest of the functionality should be a lot easier to write. Make sure the loading process is all taken care of before worrying about anything else. 1. 2. 3. The code to load 1 file containing 1 piece of data (no matter how complex that data is) should not be much different than loading 100 files, each containing 100 elements. Start by thinking about just 1 element from the file first. Do the values you read match the values in the file? What about 2 entries, does everything add up? Etc... 4. If you pass containers of data, make sure you pass them by REFERENCE, not by value. Don't create copies of anything unless you specifically need a copy. Try reading one element at a time. Read the first 4 bytes, try printing it out to the screen. Is the number something reasonable, or something that seems incorrect, like -20? If that works, move on to the next piece of data in the file. 5

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Understanding Databases Concepts And Practice

Authors: Suzanne W Dietrich

1st Edition

1119827949, 9781119827948

More Books

Students also viewed these Databases questions

Question

What are HR ethics?

Answered: 1 week ago

Question

What does corporate sustainability mean?

Answered: 1 week ago