Problem with FILE in c++

I've tried this code in binary (.bin) file, but it doesn't work on others files such as audio(e.g. .mp3), video (e.g. .avi)
wish somebody could solve this for me.
thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <conio.h>
#define length 64000 

void main(){
 FILE *fsrc, *fdes;
 long size;
 char buffer[length];
 clrscr();
 fsrc = fopen("D:\\One.bin","rb+");
 fdes = fopen("D:\\Two.bin","wb+");
 fseek(fsrc,0,2);
 size = ftell(fsrc);
 fseek(fsrc,0,0);
 while ( ftell(fsrc) < size-length ){
   fread(buffer,length,1,fsrc);
   fwrite(buffer,length,1,fdes);
   printf("\nat %ld",ftell(fsrc));
 }

 fclose(fsrc);
 fclose(fdes);
 getch();
}
> it doesn't work
¿what's the error? ¿file corrupted?

> while ( ftell(fsrc) < size-length )
¿wouldn't the last block be omitted?
I suppose that you could
1
2
count = fread( buffer, 1, length, fsrc ); //read what remains
fwrite( buffer, 1, count, fdes); //write what you read 



also, main must return int
Or do this:
1
2
3
4
5
6
    while ( !feof(fsrc) )
    {
        long result = fread(buffer,1, length,fsrc);
        fwrite(buffer, 1, result, fdes);
        printf("\nat %ld",ftell(fsrc));
    }

i've tried ftell before looping and it returns 0 (it's the current position) but after i put looping in it, ftell return -1, what does it mean?an error occurred?
Please see the reference page for ftell()
http://www.cplusplus.com/reference/cstdio/ftell/
"On failure, -1L is returned, and errno is set to a system-specific positive value."

http://www.cplusplus.com/reference/cerrno/errno/
"The particular error messages associated with values of errno can be obtained using strerror or directly printed using function perror."
thanks Chervil.. i've tried and the error string message is "Bad file number"..
Can someone tell me what does it mean?
I presume it means the file is not valid, perhaps because the file has been closed.

If you aren't sure, please share your code which gives this error.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <conio.h>
#include <errno.h>
#define length 64000

void main(){
 FILE *fsrc, *fdes;
 long size;
 char buffer[length];
 clrscr();
 fsrc = fopen("D:\\One.bin","rb+");
 fdes = fopen("D:\\Two.bin","wb+");
 fseek(fsrc,0,2);
 size = ftell(fsrc);
 fseek(fsrc,0,0);
 printf("at %ld\n",ftell(fsrc));        // <--ftell return -1 but when i close the 
					            //	looping, ftell return 0

 printf("Error : %s",strerror(errno));

 while ( ftell(fsrc) < size-length ){
// while(!feof(fsrc)){                                          <--same error
//   long result = fread(buffer,length,1,fsrc);
   fread(buffer,length,1,fsrc);
   fwrite(buffer,length,1,fdes);
   printf("\nat %ld",ftell(fsrc));
 }

 fclose(fsrc);
 fclose(fdes);
 getch();
}


i checked the file and it's valid, and i close the file at the end of the code.
Last edited on
Well, I can make a guess here. I tried the above code with two different compilers.

I had to change a couple of lines:void main() corrected this to int main(),
clrscr(); - removed this line as it is not supported by my compiler.
changed the file paths in fopen() to suit my system.

Result:
at 0
Error : Error 0

But then, having slept on this, I had a hunch. Because of the non-standard use of void main() which does not conform to the standard, main() must always return an int and the use of <conio.h> and clrscr(); I guessed that some old, non-standard compiler was being used. I found an old version of Turbo C and tried this code. Result:
at -1
Error : Bad file number

As far as i can tell, the most likely explanation in this case is the use of an obsolete compiler.

If this is indeed the case, I would strongly recommend the use of something more modern such as Code::Blocks http://www.codeblocks.org/
Topic archived. No new replies allowed.