- 論壇徽章:
- 0
|
- void do_write(int s,char *buf,int len)
- {
- int retcount;
- do{
- retcount = write(s,buf,len);
- buf += retcount;
- len -= retcount;
- }while(len > 0);
- }
- void transmit()
- {
- int fd;
- int sd = file_info.sd;
- int read_count,read_sum = 0;
- char *buf = (char*)malloc(102500);
- fd = file_info.fd = open(file_info.path,O_WRONLY | O_CREAT | O_TRUNC,0666);
- if(fd < 0){
- printf("local file open error\n");
- exit(1);
- }
- while(read_sum < file_info.size){//file_info.size為下載文件的大小
- read_count = read(sd,buf,102400);
- do_write(fd,buf,read_count);
- read_sum += read_count;
- }
- printf("read_sum:%d\n",read_sum);
- printf("file size:%d\n",file_info.size);
- free(buf);
- }
- 以上代碼用于下載文件,while循環(huán)中從網(wǎng)絡(luò)讀入數(shù)據(jù),然后寫入本地。測試發(fā)現(xiàn)讀取的數(shù)據(jù)始終比文件本來的大小多幾個字節(jié)(也就是read_sum大于file_info.size),因此下載的文件無法使用,請問我哪里寫錯了
-
復(fù)制代碼
|
|