I get this wierd output, both to file and screen. At certain points, I think at 32 or 36 bytes in, the data reads in ok but writing out seems write ahead or skip bytes, writing the *member variable oddly, with part of the next variable, *member_size added on.
I feel that maybe I am seeing some kind of struct alignment error, or perhaps I'm reading the value of the data[0].member twice and not resetting the pointer for that variable between-times, or maybe it's that I'm not accessing struct members exactly as I should. I just don't know.
Move the second "test print" to screen down the code, past another fread and it will output differently.
btw, if you run this you will probably get a "pointer p differs in levels of indirection" warning, but if this section of code is taken out it still writes to file erroneously.
#include <stdio.h>
#include <stdlib.h>
struct data_block{
char *header;
char *name;
char *name_size;
char *member;
char *member_size;
char *stream_id;
char *mo_data;
};
int main(void)
{
FILE *fpin, *fpout;
struct data_block data[1];
char *p, *start;
int i;
fpin = fopen( "somfile.in", "rb" );
fpout = fopen( "somefile.out", "wb" );
data[0].header = (char *)calloc( 24, sizeof( char ) );
data[0].name = (char *)calloc( 4, sizeof( char ) );
data[0].name_size = (char *)calloc( 4, sizeof( char ) );
data[0].member = (char *)calloc( 8, sizeof( char ) );
data[0].member_size = (char *)calloc( 4, sizeof( char ) );
data[0].stream_id = (char *)calloc( 4, sizeof( char ) );
data[0].mo_data = (char *)calloc( 4, sizeof( char ) );
// file read and screen print test
fread((&data[0].header), sizeof(char), 24, fpin);
fread((&data[0].name), sizeof(char), 4, fpin);
fread(&(data[0].name_size), sizeof(char), 4, fpin);
fread(&(data[0].member), sizeof(char), 8, fpin);
//test print 01
p = start = &data[0].member;
for(i=0; i < 8; i++)
{
printf("%c", *p++);
}
printf("\n"
//test print 02 **try moving this down**
p = start;
for(i=0; i < 8; i++)
{
printf("%c", *p++);
}
printf("\n"
fread(&(data[0].member_size), sizeof(char), 4, fpin);
fread(&data[0].stream_id, sizeof(char), 4, fpin);
fread(&data[0].mo_data, sizeof(char), 4, fpin);
// file write
//fwrite(&data[0].header, sizeof(char), 24, fpout); //don't write this because the fwrite(&data[0].name, sizeof(char), 4, fpout); //error's further on
fwrite(&data[0].name_size, sizeof(char), 4, fpout);
fwrite(&(data[0].member), sizeof(char), 8, fpout);
fwrite(&data[0].member_size, sizeof(char), 4, fpout);
fwrite(&data[0].stream_id, sizeof(char), 4, fpout);
fwrite(&data[0].mo_data, sizeof(char), 4 , fpout);
// close files
//fflush(fpout);
fclose(fpin);
fclose(fpout);
} //end main
I feel that maybe I am seeing some kind of struct alignment error, or perhaps I'm reading the value of the data[0].member twice and not resetting the pointer for that variable between-times, or maybe it's that I'm not accessing struct members exactly as I should. I just don't know.
Move the second "test print" to screen down the code, past another fread and it will output differently.
btw, if you run this you will probably get a "pointer p differs in levels of indirection" warning, but if this section of code is taken out it still writes to file erroneously.
#include <stdio.h>
#include <stdlib.h>
struct data_block{
char *header;
char *name;
char *name_size;
char *member;
char *member_size;
char *stream_id;
char *mo_data;
};
int main(void)
{
FILE *fpin, *fpout;
struct data_block data[1];
char *p, *start;
int i;
fpin = fopen( "somfile.in", "rb" );
fpout = fopen( "somefile.out", "wb" );
data[0].header = (char *)calloc( 24, sizeof( char ) );
data[0].name = (char *)calloc( 4, sizeof( char ) );
data[0].name_size = (char *)calloc( 4, sizeof( char ) );
data[0].member = (char *)calloc( 8, sizeof( char ) );
data[0].member_size = (char *)calloc( 4, sizeof( char ) );
data[0].stream_id = (char *)calloc( 4, sizeof( char ) );
data[0].mo_data = (char *)calloc( 4, sizeof( char ) );
// file read and screen print test
fread((&data[0].header), sizeof(char), 24, fpin);
fread((&data[0].name), sizeof(char), 4, fpin);
fread(&(data[0].name_size), sizeof(char), 4, fpin);
fread(&(data[0].member), sizeof(char), 8, fpin);
//test print 01
p = start = &data[0].member;
for(i=0; i < 8; i++)
{
printf("%c", *p++);
}
printf("\n"
//test print 02 **try moving this down**
p = start;
for(i=0; i < 8; i++)
{
printf("%c", *p++);
}
printf("\n"
fread(&(data[0].member_size), sizeof(char), 4, fpin);
fread(&data[0].stream_id, sizeof(char), 4, fpin);
fread(&data[0].mo_data, sizeof(char), 4, fpin);
// file write
//fwrite(&data[0].header, sizeof(char), 24, fpout); //don't write this because the fwrite(&data[0].name, sizeof(char), 4, fpout); //error's further on
fwrite(&data[0].name_size, sizeof(char), 4, fpout);
fwrite(&(data[0].member), sizeof(char), 8, fpout);
fwrite(&data[0].member_size, sizeof(char), 4, fpout);
fwrite(&data[0].stream_id, sizeof(char), 4, fpout);
fwrite(&data[0].mo_data, sizeof(char), 4 , fpout);
// close files
//fflush(fpout);
fclose(fpin);
fclose(fpout);
} //end main