A
Andrew
This is a homework question that has gone passed it's due date,
however I still want to figure it out. This program initializes three
structures, writes them to a binary file, reads them back out and is
supposed to reverse the Endian. I cannot get the
ReverseEndian(&TempStore->flt, sizeof(TempStore->flt));
function call to pass the pointer to
void ReverseEndian(void *ptr, size_t size)
I did have it pass a couple of times so I am suspicious of the
compiler(.NET), but I am a newbie so I need someone to throw some
experience at this to make sure I didn't over look the obvious.
Thanks for any help you can offer.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NumOfWriteObjects 1
#define BUFFSIZE 256
#define FileSize 20
void ReverseEndian(void *ptr, size_t size);
struct Test
{
float flt;
double dbl;
void *vptr;
}Test;
int main(void)
{ //Declare and initialize array
struct Test TestArray[3] = {{12.3F, 6666L, "old"}, {45.6F, 7777L,
"houses"}, {78.9F, 8888L, "always"}};
int count = sizeof(TestArray)/sizeof(TestArray[0]);
FILE *flptr;
char BinaryFile[FileSize];
struct Test TempStore[BUFFSIZE];
printf("Please enter a binary storage file in XXXXXX.bin format: ");
scanf("%s", &BinaryFile);
if((flptr = fopen(BinaryFile, "wb")) == NULL)
{
printf("Error opening write file\n\n");
return(EXIT_FAILURE);
}
if(count != fwrite(TestArray, sizeof(TestArray[0]), count, flptr))
{
printf("Error storing Array\n\n");
return(EXIT_FAILURE);
}
fclose(flptr);
if((flptr = fopen(BinaryFile, "rb")) == NULL)
{
printf("Error opening read file\n\n");
return(EXIT_FAILURE);
}
while(fread(TempStore, sizeof(Test), 1, flptr))
{
ReverseEndian(&TempStore->flt, sizeof(TempStore->flt));
ReverseEndian(&TempStore->dbl, sizeof(TempStore->dbl));
ReverseEndian(&TempStore->vptr, sizeof(TempStore->vptr));
}
fclose(flptr);
return(EXIT_SUCCESS);
}
void ReverseEndian(void *ptr, size_t size)
{
char temp = '0';
char TempStr[BUFFSIZE] = {(char )ptr};
unsigned int x, y;
for(x = 0, y = size - 1; x < (size / 2); ++x, --y)
{
temp = TempStr[y];
TempStr[y] = TempStr[x];
TempStr[x] = temp;
}
ptr = (void *)TempStr;
}
however I still want to figure it out. This program initializes three
structures, writes them to a binary file, reads them back out and is
supposed to reverse the Endian. I cannot get the
ReverseEndian(&TempStore->flt, sizeof(TempStore->flt));
function call to pass the pointer to
void ReverseEndian(void *ptr, size_t size)
I did have it pass a couple of times so I am suspicious of the
compiler(.NET), but I am a newbie so I need someone to throw some
experience at this to make sure I didn't over look the obvious.
Thanks for any help you can offer.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NumOfWriteObjects 1
#define BUFFSIZE 256
#define FileSize 20
void ReverseEndian(void *ptr, size_t size);
struct Test
{
float flt;
double dbl;
void *vptr;
}Test;
int main(void)
{ //Declare and initialize array
struct Test TestArray[3] = {{12.3F, 6666L, "old"}, {45.6F, 7777L,
"houses"}, {78.9F, 8888L, "always"}};
int count = sizeof(TestArray)/sizeof(TestArray[0]);
FILE *flptr;
char BinaryFile[FileSize];
struct Test TempStore[BUFFSIZE];
printf("Please enter a binary storage file in XXXXXX.bin format: ");
scanf("%s", &BinaryFile);
if((flptr = fopen(BinaryFile, "wb")) == NULL)
{
printf("Error opening write file\n\n");
return(EXIT_FAILURE);
}
if(count != fwrite(TestArray, sizeof(TestArray[0]), count, flptr))
{
printf("Error storing Array\n\n");
return(EXIT_FAILURE);
}
fclose(flptr);
if((flptr = fopen(BinaryFile, "rb")) == NULL)
{
printf("Error opening read file\n\n");
return(EXIT_FAILURE);
}
while(fread(TempStore, sizeof(Test), 1, flptr))
{
ReverseEndian(&TempStore->flt, sizeof(TempStore->flt));
ReverseEndian(&TempStore->dbl, sizeof(TempStore->dbl));
ReverseEndian(&TempStore->vptr, sizeof(TempStore->vptr));
}
fclose(flptr);
return(EXIT_SUCCESS);
}
void ReverseEndian(void *ptr, size_t size)
{
char temp = '0';
char TempStr[BUFFSIZE] = {(char )ptr};
unsigned int x, y;
for(x = 0, y = size - 1; x < (size / 2); ++x, --y)
{
temp = TempStr[y];
TempStr[y] = TempStr[x];
TempStr[x] = temp;
}
ptr = (void *)TempStr;
}