点击(此处)折叠或打开
- #include <sys/types.h>
- #include <signal.h>
- #include <stdio.h>
- #include <string.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <sys/stat.h>
- #define BLKSIZE 255
- int main(int argc, char *argv[])
- {
-
- int fdr,fdw;
- int r_size;
- int iFlen;
- int iBlock = 0;
- char temp[255];
- char filepath[255];
- char fileto[255];
-
- struct stat stbuf;
-
- printf("please input the filepath:");
-
- // scanf("%s",filepath);
- strcpy(filepath, argv[1]);
- // fdr = open(argv[1], O_RDONLY|O_BINARY, S_IREAD);
- // fdr = _open(argv[1], O_BINARY);
- fdr = _open(argv[1], O_RDONLY | O_BINARY, S_IWRITE | S_IREAD);
- // if((fdr=open(filepath,O_RDONLY))==-1)
- if( fdr==-1 )
- {
- printf("file not found,open failed!");
- return -1;
- }
- else
- {
-
- // iFlen = lseek( fdr, 0L, SEEK_END );
- stbuf.st_size = 0 ;
- stat(filepath,&stbuf);
- iFlen = stbuf.st_size;
- iBlock = BLKSIZE;
- fdw = _open(argv[2], O_WRONLY|O_CREAT|O_BINARY , 0777);
- // fdw = open(argv[2], O_RDONLY|O_BINARY, S_IREAD);
- for(;;)
- {
- if(iFlen >BLKSIZE)
- iBlock = BLKSIZE;
- else
- iBlock = iFlen ;
- memset(temp, 0x00, sizeof(temp));
- r_size=_read(fdr,temp,iBlock);
- if(r_size != iBlock)
- {
- printf("读数据失败!!!!!!!!!!!![%d, %s]\n", errno, strerror(errno));
- printf("%d, %d\n", r_size , iBlock);
- close(fdr);
- break;
- }
- _write(fdw, temp,iBlock);
- iFlen -=BLKSIZE;
- if(iFlen <= 0 )
- {
- printf("完成读取文件!!!!\n");
- break;
- }
- }
- close(fdw);
- close(fdr);
- printf("\n");
- printf("uuuuuuuuuu,[%d,%s\n", iFlen, temp);
- /*
- while(r_size=read(fdr,temp,255) > 0)
- {
- printf("uuuuuuuuuu,%s\n", temp);
- }
- close(fdr);
- sprintf(fileto,"file-rw.txt");
- if((fdw=open(fileto,O_WRONLY|O_CREAT,0640))==-1)
- {
- printf("open failed!");
- return -1;
- }
- else
- {
- int w_size=write(fdw,temp,r_size);//相当于复制了所输入文件.txt中的255个大小的字符串到新文件file-rw.txt中
- close(fdw);
- printf("have successfully copied 255 chars from \"%s\" to \"%s\"\n",filepath,fileto);
- }
- */
-
- }
-
- return 0;
-
- }