C++ Binary File IO - 69036

Solution Posted by


Rating : (19)A+
Solution Detail
Price: $20.00
Request Description

File Reading and Writing

This assignment consists of reading three files and writing out the results to the console. The three files can be found on Moodle: text.bin, bin_data.bin, and mixed.bin.

1) Reading text from a file

You are to open the file “text.bin” for reading. It contains a single line of text “Mary had a little lamb”. You are to read this file, read the words, then write them to the console in reverse order. The output should read

“lamb little a had Mary”.

Hint: Store the words in an array as you read them.

2) Reading binary data from a file

You are to open the file “bin_data.bin” for reading. It contains binary data written using the following data format:

struct binary {int a; float b; char letters[6];}

That is, there are three records in the array. Each record contains one int, one float, and 6 letters. You are to read the data and output it to the console. The output should look like (depending on formatting):

3 3.14 pi 2 1.414 sqrt 42 42.0 towel

Hint: Create a buffer of type binary and read the data into it. Then you can access it by bufferName.a, bufferName.b, bufferName.letters. Read and output one buffer full at a time until you reach the end of the file.

3) Reading and parsing mixed data

You are to open the file “mixed.bin” for reading. It contains ascii data that is a mixture of words and integers with spaces between each item. There are a total of four lines in the file, the fourth line contains “00”. You should read a line of data, then break it into words and numbers, saving them in a string array and an integer array. You can ignore any spaces. When you are done reading the file, you should write out the saved data. The output should look like:

Horse House Elephant Dog Car Cat Mouse Rat Cow Pig Chicken

36 32 1 2 4 47 19 19 11 0

Hint: Read each line into a buffer. Then walk down the buffer one character at a time. If the character is a letter, then extract a word. If the character is a digit, then extract a number. If it is a space, then increment the index by one.

Solution Description

C++ Visu