Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

INSTRUCTOR SUPPLIED SOURCE CODE: #define INSTRUCTOR_FILE #ifdef INSTRUCTOR_FILE #include #include #include C2A7E4_Test-Driver.h FILE *OpenTemporaryFile(void); struct Test *ReverseMembersEndian(struct Test *ptr); struct Test *ReadStructures(struct Test *ptr, size_t

image text in transcribed

image text in transcribed

image text in transcribed

INSTRUCTOR SUPPLIED SOURCE CODE:

#define INSTRUCTOR_FILE #ifdef INSTRUCTOR_FILE

#include #include

#include "C2A7E4_Test-Driver.h"

FILE *OpenTemporaryFile(void); struct Test *ReverseMembersEndian(struct Test *ptr); struct Test *ReadStructures(struct Test *ptr, size_t count, FILE *fp); struct Test *WriteStructures(const struct Test *ptr, size_t count, FILE *fp);

#define Elements(array) (sizeof(array)/sizeof((array)[0]))

typedef unsigned char UChar;

int main(void) { const char * const MESSAGE = "Error in WriteStructures or ReadStructures or both: "; UChar *cp; int elemNo; size_t byteNo; FILE *temporaryFile; int gotError = 0;

struct Test x[] = // an array of structures { // declare and initialize {23.6F, -425e-6, (void *)0x2345}, // structure 0 {2, 1, 0}, // structure 1 {-6, 3.3, NULL} // structure 2 };

struct Test y[Elements(x)] = // an array of structures { {123.456F, 789.12, (void *)0x9876}, // structure 0 {69.0F, 24, (void *)0x1928A}, // structure 1 {-59.2F, 89.48e-16, (void *)0xABC} // structure 2 };

size_t fileSize;

// file for unaltered structures temporaryFile = OpenTemporaryFile(); // write multiple structures WriteStructures(x, Elements(x), temporaryFile); // back to beginning rewind(temporaryFile); for (fileSize = 0; fgetc(temporaryFile) != EOF; ++fileSize) ; if (fileSize != sizeof(x)) { printf( "Error in WriteStructures: The wrong number of bytes were written. " "This sometimes indicates that you are attempting to write the " "members of a structure to a file individually and possibly in " "\"text\" rather than writing the entire structure at once in " "\"binary\". "); gotError = 1; } rewind(temporaryFile); // read multiple structures ReadStructures(y, Elements(y), temporaryFile); printf(" ");

// Structure member values before and after write/read. for (elemNo = 0; elemNo

if (gotError) return EXIT_SUCCESS;

// Print bytes in structures before & after reversal. printf("Structure bytes before (1st line) & after (2nd line) reversal: "); for (elemNo = 0; elemNo

return EXIT_SUCCESS; } #endif

C2A7E4 (6 points - Program) Exclude any existing source code files that may already be in your IDE project and add three new ones, naming them C2A7E4_ReverseEndian.c, C2A7E4_ProcessStructures.c, and C2A7E4_OpenTemporary File.c. Also add instructor-supplied source code files C2A7E4_Test-Driver.h and C2A7E4_main-Driver.c. Do not write a main function! main already exists in the instructor-supplied implementation file and it will use the code you write. The purpose of this exercise is to illustrate the endian conversion of arbitrary scalar objects within an aggregate type, such as a structure or class. If it is arbitrarily assumed that a long is 4 bytes and a short is 2 bytes, a structure defined and initialized as struct { long height; short width, depth; } box = {0x812345671, ex89ab, excdef}; and written into a file using fwrite(&box, sizeof(box), 1, fp); will be written exactly how it appears in memory, which is in one of the two following orders, depending upon the machine's endianness. There can be an arbitrary amount of implementation-dependent padding after any members: Big Endian Little Endian First lyte in memory & in the file: 01 67 23 45 67 ----- possible padding here --- 89 ab ab 89 ----- possible padding here ----- cd ef ----- possible paddling here ---- 45 23 01 ef cd A structure of data type struct Test is what you will be using in this exercise. It is defined as struct Test { float flt; double dbl; void *vp; }; in instructor-supplied header file C2A7E4_Test-Driver.h Any file that uses this data type must include this header file using #include. Do not define this data type separately from this header file. File C2A7E4_Reverse Endian.c must contain a copy of the same ReverseEndian function you wrote for the previous exercise. File C2A7E4_OpenTemporaryFile.c must contain a function named OpenTemporaryFile. OpenTemporaryFile syntax: FILE *OpenTemporaryFile(void); Parameters: none Synopsis: Opens a temporary file using the standard library tmpfile function. If the open fails an error message is output to stderr and the program is terminated with an error exit code. Return: A pointer to the open file if the open succeeds; otherwise, the function does not return. File C2A7E4_ProcessStructures.c must contain functions named ReverseMembersendian, Readstructures, and Writestructures. ReverseMembersendian syntax: struct Test *ReverseMembersendian(struct Test *ptr); Parameters: ptr -a pointer to the structure whose members' endiannesses are to be reversed Synopsis: Calls function ReverseEndian once for each member of the structure in ptr, thereby converting each from big endian to little endian or vice versa. A loop cannot be used to access the members nor can ReverseEndian reverse the endianness of the entire structure at once. Return: ptr Readstructures syntax: struct Test *Readstructures(struct test *ptr, size_t count, FILE *fp); Parameters: ptr-a pointer to where the structure(s) that are read will be placed in memory count - the number of structures to read fp-a pointer to an open binary file containing the structure(s) to be read. Synopsis: Using no loops and no recursion it reads count structures from fp and stores them in memory starting at address ptr. If count structures cannot be read an error message is output to stderr and the program is terminated with an error exit code. Return: ptr if count structures are read; otherwise, the function does not return. Writestructures syntax: struct Test *Writestructures (const struct Test *ptr, size_t count, FILE *fp); ptr - a pointer to where the structure(s) that are written will come from in memory count - the number of structures to write fp-a pointer to an open binary file into which the structure(s) will be written. Synopsis: Using no loops and no recursion it reads count structures from memory starting at address ptr and writes them to the file fp. If count structures cannot be written an error message is output to stderr and the program is terminated with an error exit code. Return: ptr if count structures are written; otherwise, the function does not return. Parameters: Do not use dynamic memory allocation. Do not make any assumptions about the number of bytes in any data type. Do not make any assumptions about the presence, absence, value, or amount of padding. Please answer the following questions about your results and place these answers as comments in the title block of the file containing the ReverseMembersendian function: 1. Were the results you got correct for your implementation? 2. How many padding bytes were in your structure

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

Database 101

Authors: Guy Kawasaki

1st Edition

0938151525, 978-0938151524

More Books

Students also viewed these Databases questions