fwrite in binary mode  
Author Message
MMMalik





PostPosted: Visual C++ Language, fwrite in binary mode Top

I have a binary file that i am reading in an array and then after doing some processing i want to write it into a new binary file. The reading and the processing is working fine but writing back is not working.

FILE *fptr;

FILE *fptr2;

unsigned short *ds;

unsigned short *ds2;

fptr = fopen("C:\\abc.dat","rb");

fptr2 = fopen("C:\\abs2.dat","wb");

fread((void*)ds, totalSize, sizeof(unsigned short), fptr);             // total size is the size of the data and is correct

processds(ds, ds2);        // ds is processed and copied into ds2 and works fine

fwrite(ds2, sizeof(unsigned short), totalSize, fptr2);      // a new file is created bust has no bytes in there

 

how can i write it back

thanks in advance

best wishes

 

 



Visual C++3  
 
 
Mike Danes





PostPosted: Visual C++ Language, fwrite in binary mode Top

How you open the file for writing And are you sure totalSize does not change between fread and fwrite


 
 
MMMalik





PostPosted: Visual C++ Language, fwrite in binary mode Top

hi

i have included the code for opening the files in my first post. yeah total size remains the same

any idea whats going wrong

regards


 
 
Brian Kramer





PostPosted: Visual C++ Language, fwrite in binary mode Top

Although you say you're having success with your code, I see that you haven't allocated any space for ds and ds2. If this isn't the real version of your code, could you please provide it
 
 
MMMalik





PostPosted: Visual C++ Language, fwrite in binary mode Top

This is how i am allocating sizes:

ds = new unsigned short [totalSize];

ds2 = new unsigned short [totalSize];

actually this is part of a very lenghty project where i am making changes.

so i am picking code and placing it here.

Reading, and processing of the data works super fine. i have tested ds2 and there is right data in there.

only when i am putting the stuff from ds2 to file, it starts making problems

:(


 
 
Brian Kramer





PostPosted: Visual C++ Language, fwrite in binary mode Top

If its a sneaky bug, then you should give us the benefit of full disclosure of the code. :)


 
 
Mike Danes





PostPosted: Visual C++ Language, fwrite in binary mode Top

Did you check the return value of fwrite


 
 
MMMalik





PostPosted: Visual C++ Language, fwrite in binary mode Top

12 classes and above 4000 lines of code!!

can you give it another try!!

even when i did :

int tt = fwrite(ds2, sizeof(unsigned short), totalSize, fptr2);

tt was equal to totalSize

but the file is still of 0 KB.

now only the following code is there and the problem persists:

FILE *fptr2;

int main (int argc, char** argv){

int abc[] = {1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9};

if((fptr2 = fopen("C:\\work\\datasets\\mine.dac","wb"))==NULL) {

printf("Cannot open file.\n"); // it is not printed

}

int tt = fwrite(abc, sizeof(int), 90, fptr2);

if ( tt != 90)

printf("Cannot open file.\n"); // it is not printed

fclose(fptr2);

}

any ideas


 
 
MMMalik





PostPosted: Visual C++ Language, fwrite in binary mode Top

hey the above code executes fine  

 

is there any difference between

unsigned short *ds = ..............;

unsigned short ds[] = .............

 

are we reaching some solution


 
 
Brian Kramer





PostPosted: Visual C++ Language, fwrite in binary mode Top

Your code looks fine and it works on my machine (after renaming the path). What is the value of errno after fwrite is called

Try:

errno = 0;
int tt = fwrite(abc, sizeof(int), 90, fptr2);
printf(
"error=%s\n",strerror(errno));

My output: error=No error


 
 
MMMalik





PostPosted: Visual C++ Language, fwrite in binary mode Top

it now works fine...believe me the previous code that i earlier had is doing fine now

i did not do any changes

i dont know what is my machine doing

but thanks a lot lot :) :)


 
 
Brian Kramer





PostPosted: Visual C++ Language, fwrite in binary mode Top

Well it's good to know you're out of crisis mode. I'll mark this thread as answered now.