Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Corrupted in File I/O operations 1

Status
Not open for further replies.

netprik1

Programmer
Jul 19, 2007
7
US
So here I am running regular C code that looks like this:

(I am new to these forums and don't know how to do the code box, I'll try my best)

<code>

#include <stdio.h>
#include <stdlib.h>

#define xImageSize 435
#define yImageSize 383

typedef struct {
unsigned char** data;
unsigned int xSize;
unsigned int ySize; } u_char_2Darray;

typedef struct {
unsigned char *buffer1, *buffer2;
u_char_2Darray *image1Array, *image2Array;
} memory;

//function prototypes
void MIF (memory *memoryMaster);
void allocateMemory (memory *memoryMaster);
u_char_2Darray* allocate2D_U_CharArray (unsigned int xs, unsigned int ys);

int main(int argc, char *argv[])
{
memory memoryMaster;
allocateMemory (&memoryMaster);
MIF (&memoryMaster);
return 0;
}

void allocateMemory (memory *memoryMaster) {
memoryMaster->buffer1 = (unsigned char*) malloc (sizeof (unsigned char)*xImageSize*yImageSize);
memoryMaster->buffer2 = (unsigned char*) malloc (sizeof (unsigned char)*xImageSize*yImageSize);
memoryMaster->image1Array = allocate2D_U_CharArray (xImageSize, yImageSize);
memoryMaster->image2Array = allocate2D_U_CharArray (xImageSize, yImageSize); }

u_char_2Darray* allocate2D_U_CharArray (unsigned int xs, unsigned int ys) {
int i;
//allocate the struct itself
u_char_2Darray *prod = (u_char_2Darray*) malloc(sizeof (u_char_2Darray));
//allocate the array of pointers to arrays
prod->data =(unsigned char**) malloc(sizeof(unsigned char*)*ys);
//allocate each of the arrays
for (i=0;i<ys;i++) {
prod->data = (unsigned char*) malloc(sizeof(unsigned char)*xs); }
prod->xSize = xs;
prod->ySize = ys;
return prod; }

void MIF (memory *memoryMaster) {

FILE *f1, *f2;
unsigned char **image1, **image2;
unsigned char *buffer1, *buffer2;
//looping variables
int r,p;

image1 = memoryMaster->image1Array->data;
image2 = memoryMaster->image2Array->data;
buffer1 = memoryMaster->buffer1;
buffer2 = memoryMaster->buffer2;

f1 = fopen("image1.txt","r");
f2 = fopen("image2.txt", "r");
if ((f1 == NULL) || (f2 == NULL)) {
printf("Error opening files!\n");
return;
}

fread (buffer1,sizeof (unsigned char),xImageSize*yImageSize,f1);
fread (buffer2,sizeof (unsigned char),xImageSize*yImageSize,f2);

//copy these into the 2D arrays
for (r=0;r<yImageSize;r++) {
for (p=0;p<xImageSize;p++) {
image1 [r][p] = buffer1 [r+p*yImageSize];
image2 [r][p] = buffer2 [r+p*yImageSize]; }}

fclose (f1);
fclose (f2);

f1 = fopen("image1mod.txt","w");
f2 = fopen("image2mod.txt", "w");
if ((f1 == NULL) || (f2 == NULL)) {
printf("Error opening files!\n");
return;
}

for (p=0;p<xImageSize;p++) {
for (r=0;r<yImageSize;r++) {
fprintf (f1, "%c", image1 [r][p]);
fprintf (f2, "%c", image2 [r][p]);}}

fclose (f1);
fclose (f2);

}

</code>

What this should do:
produce an exact copy of image1.txt in image1mod.txt and a copy of image2.txt in image2mod.txt. However, this is not quite what happens. Using MATLAB, I have produced visualizations of the data so that you can see how the initial data (image1.txt) and the final data (image1mod.txt) looks like.

They're on this page:


Strange, wouldn't you say? I tried this with another compiler and I get the same results...

Can anyone help? Thanks a lot!
 
Use the "b" mode to fopen() files in Binary mode instead of Text mode.
Code:
f1 = fopen( "image1.txt", "rb" );
f2 = fopen( "image2.txt", "rb" );
f1 = fopen( "image1mod.txt", "wb" );
f2 = fopen( "image2mod.txt", "wb" );
BTW, it's [ignore]
Code:
[/ignore] instead of [ignore]<code></code>[/ignore]
 
It's fixed! Thanks a ton, you just made my day! I was going crazy about this yesterday...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top