C/File Data Storage!
Expert: Prince M. Premnath - 9/26/2007
Question Hi,
How are you? I'd like to kindly ask for your help with the following problem
(I'm only showing the relevant parts of my program)
First I need to divide a binary file into two files. I do it like this:
static int *p;
while (feof(oldfile)==0)
{
if (length<1000)
{
fread(&p, 1, 1, oldfile);
fwrite(&p, 1, 1, newfile1);
}
if (length >=1000)
{
fread(&p, 1, 1, oldfile);
fwrite(&p, 1, 1, newfile2);
}
length +=1;
}
Then I need to put the two new files back together (merged file):
while(feof(newfile1)==0)
{
fread(&p, 1, 1, newfile1);
fwrite(&p, 1, 1, mergefile);
}
while(feof(newfile2)==0)
{
fread(&p, 1, 1, newfile2);
fwrite(&p, 1, 1, mergefile);
}
I treat all files like binary files when I open them. The problem I have is that in my new file (mergefile) I get
doubling of bytes exactly where the two files (newfile1 and newfile2) were joined together. Also I get additional spaces at the end
of the new file (mergefile). Do you know why? Is the feof() a problem or do I have a prblem with my pointers? Why don't I get
perfect fusion of the two files.
Sincerely,
Tom
AnswerHi dear Tom !
Yep , im fine hope u too . really merging of files won't increase the file size !If so then there might be some errors with your algorithm .
instead of fwrite() putc() will do the job efficiently also binary files treat each and ever character as 'char ' type so thats good to use char data type to read and write the data's over the binary file ( sure thats why your file size is doubled and extra spaces )
let me revise this code !
char ch;
while (feof(oldfile)==0)
{
ch = getc(oldfile);
if( length <= 1000)
putc(ch , newfile1);
.
.
.
.
.
.
.
}
Im sure this code will really help you out !
If i got some leisure time sure ill send some code excerpts related to ur problem
Thanks and Regards !
Prince M. Premnath