char pointer issue

  • Thread starter Thread starter Anderson
  • Start date Start date
A

Anderson

Hi,all
I use the following code to read 10000 bytes from a file(fp1) and write
them to another file(fp2).

char *pStr = new char[10001];
fread(pStr,dw_size,1,fp1);
int i = 0;
while(i<10000)
{
fwrite(pStr,1,1,fp2);
pStr++;
}

I found only 1682 bytes was written to fp2!

Could any body tell me why?

Thanks in advance!

Anderson
 
I use the following code to read 10000 bytes from a file(fp1) and write
them to another file(fp2).

char *pStr = new char[10001];
fread(pStr,dw_size,1,fp1);

what is the value of dw_size here?
int i = 0;
while(i<10000)
{
fwrite(pStr,1,1,fp2);
pStr++;
}
1) i never changes, so this is an endless loop. Is this really the all of
the code in your function?
2) you write 1 byte at a time. why not write the whole buffer at once?
I found only 1682 bytes was written to fp2!

Apart from the things I already mentioned, You really should check the
return values that you get from the different IO functions. how else would
you know if there was a problem or not?

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
Thanks Bruno for metioned these points!

Bruno van Dooren said:
I use the following code to read 10000 bytes from a file(fp1) and
write
them to another file(fp2).

char *pStr = new char[10001];
fread(pStr,dw_size,1,fp1);

what is the value of dw_size here?
int i = 0;
while(i<10000)
{
fwrite(pStr,1,1,fp2);
pStr++;
}
1) i never changes, so this is an endless loop. Is this really the all of
the code in your function?
2) you write 1 byte at a time. why not write the whole buffer at once?
I found only 1682 bytes was written to fp2!

Apart from the things I already mentioned, You really should check the
return values that you get from the different IO functions. how else would
you know if there was a problem or not?

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
Back
Top