View Full Version : probs in copying file in binary format
hello all,
i have some probs with file handleing , but as the file section is locked, could not post there. the prob is:-
i want just to copy a file into another, i have coded it , and works fine for txt files, but when copying a binary file, it gives trouble, it copies but the new copy doesnot function properly, donot know y, y?
i have used open(), in dos/c (borland c++ 3.1) we have a flag O_BINARY, which ensured that the copying works fine for binary file, but i cannot find O_BINARY flag in cc, is it there?
else what is the option,
i am using read and write to copy file, but still ..
what to use?
dev
RobSeace
04-08-2003, 01:37 PM
Unix OSs make no distinction between binary and text files; they're
all the same, and always treated as binary ones are on lame OSs that
support the distinction...
So, basically, there should be no problem... If you're seeing some
problem, I would guess there must be some bug in your code, but I'm
not sure why it would only affect non-text files... What exactly is
happening??
8
9 main()
10 {
11 int fd,fs;
12 int filesize;
13 struct stat filestat;
14 char fdname[20],fsname[20];
15 void *buf[10];
16
17 printf("\n enter the source name : ");
18 scanf("%s",fsname);
19
20 printf("\n enter the des name : ");
21 scanf("%s",fdname);
22
23 fs=open(fsname,O_RDONLY);
24 if(fs==-1) //error
25 {
26 perror("\n error in opening sorce : ");
27 exit(-1);
28 }
29
30 fd=open(fdname,O_WRONLY|O_CREAT);
31 if(fd==-1)
32 {
33 perror("\n error in opening destination : ");
34 exit(-1);
35 }
36
37 if( stat(fsname,&filestat)== -1)
38 {
39 perror("\n error in stat : ");
40 exit(-1);
41 }
42
43 for(filesize=1;filesize<=filestat.st_size;filesize ++)
44 {
45 read(fs,buf,1);
46 write(fd,buf,1);
47
48 }
49 //write(fd,buf,1);
50
51 close(fs);
52 close(fd);
53 }
so what could be wrong, i feel its in the number of bytes i am writing , but there cannot be any prob there, so where?
dev
RobSeace
04-08-2003, 09:18 PM
You still didn't say exactly what was happening... Is the copy too short,
or too long, or does it contain data not in the original, or what?
But, that code is highly odd... ;-) Why do you have "buf" be an array of
10 void*'s?? It looks like you're just reading a single byte into it... But,
whatever; that should still work like that, anyway... It's just strange... ;-)
But, ONE issue that's definitely wrong is that you're missing your third
argument to open() in the second call... You have to pass the third arg
whenever you pass in O_CREAT...
However, even buggy like that, it works relatively ok for me... It just
creates the copy with wacked out permissions, because it grabbed random
crap of the stack for the third arg... But, the copy it created was fine...
hi Rob,
well coding is funny, it gave probs yesterday, today morning its working perfectly, with no probs at all, donot know what happened.
the prob it gave yesterday was that:-
- i complie the code- cc filecopy.c -lc
- i get a.out
- i run a.out
- it asks for source and destination files
- i give in source a.out itself and in destination b.out
- i run b.out
- again it asks for src and dest
- in src i give b.out and in dest c.out
- BUT..., it program just does nothing and like in a infinite loop , doesnot return control
thats it
.
so may be when this prob surfaces again, i will ask u guys again.
or if u can figure it out now , plz tell.
dev
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.