user-0 提问: C语言编程：用二进制打开一个*dat文件，取出1K

回复:
user: user-1
content: 楼上正解啊，不难

user: user-2
content: 谢邀，已经有解，露个脸^_^

user: user-3
content: 把你自己 写的代码拿出来看看.  比如文件大小为8K，要取出从第6.5K，到7.5K的1K。 fread fseek(pFile, 6.5*1024, SEEK_SET); //从文件起始位置移动6.5k, 当然, 你自己计算的时候, 要写成字节 fread(buf, 1,  1*1024, pFile); //读取1K数据到buf中. fclose

user: user-4
content: 不判断每个函数的返回值≡作死

user: user-5
content: 引用 9 楼 mujiok2003 的回复:[quote=引用 6 楼 helloya123 的回复:] 你这个代码我怎么看不懂啊？ 比如文件大小为8K，要取出从第6.5K，到7.5K的1K。 直接调用吧: demo.exe 6500 [/quote] demo.exe 6656

user: user-5
content: 引用 6 楼 helloya123 的回复:你这个代码我怎么看不懂啊？ 比如文件大小为8K，要取出从第6.5K，到7.5K的1K。 直接调用吧: demo.exe 6500

user: user-0
content: 楼上回复太复杂了。应该用fopen、fseek、fread就能解决吧。我没调试通，请教各位前辈。

user: user-4
content: #include <sys\stat.h> #include <io.h> #include <fcntl.h> #include <share.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <string.h> #define MAX_CLU_BYTES 65536 FILE *fo; int fh; __int64 offs,offs1; __int64 rvi64; int rv,wrv; unsigned char buf[MAX_CLU_BYTES]; char ofn[_MAX_PATH]; char offstr[80]; void strcpybutcomma(char *t,char *s) {     char c;      while (1) {         c = *s++;         if (','!=c) *t++ = c;         if (0==c) break;     } } void main(int argc,char **argv) {     if (argc<3) {         printf("Copy File Tail.\n");         printf("Usage:\n");         printf("    cft filename.ext offset_begin[-offset_end]\n");         printf("Copy filename.ext offset_begin[-offset_end] to offset_begin[-offset_end]-filename.ext\n");         printf("Note: Byte at offset_end is NOT included.\n");         printf("Example:\n");         printf("    cft abc.rar 12345\n");         printf("Copy abc.rar offset 12345-end to 12345-abc.rar\n");         printf("    cft abc.rar 123-12345\n");         printf("Copy abc.rar offset 123-12345 to 123-12345-abc.rar\n");         printf("    cft abc.rar 0xAB-0xCD\n");         printf("Copy abc.rar offset 0xAB-0xCD to 0xAB-0xCD-abc.rar\n");         return;     }     strcpybutcomma(offstr,argv[2]);     rv=sscanf(offstr,"%I64i-%I64i",&offs,&offs1);     if (rv==0) {         printf("offset %s is not number\n",argv[2]);         return;     }     fh=_sopen(argv[1],_O_BINARY|_O_RDONLY|_O_RANDOM,_SH_DENYNO);     if (fh==-1) {         printf("_sopen %s errno=%d\n",argv[1],errno);         return;     }     if (rv==1) {         offs1=_filelengthi64(fh);         if (offs1==-1i64) {             printf("%I64=_filelengthi64 errno=%d\n",offs1,errno);             _close(fh);             return;         }     } else {//rv==2         if (offs1<offs) {             printf("%s offset_begin>offset_end error\n",argv[2]);             _close(fh);             return;         }     }     rvi64=_lseeki64(fh,offs,SEEK_SET);     if (rvi64!=offs) {         printf("%I64u=_lseeki64 %I64u errno=%d\n",rvi64,offs,errno);         _close(fh);         return;     }     sprintf(ofn,"%s-",offstr);     strcat(ofn,argv[1]);     fo=fopen(ofn,"wb");     if (fo==NULL) {         _close(fh);         printf("fopen %s error\n",ofn);         return;     }     cprintf("\n%I64u\r",offs);     while (1) {         rv=_read(fh,buf,(unsigned int)__min(offs1-offs,MAX_CLU_BYTES));         if (rv==0) break;//         if (rv<0) {             fclose(fo);             _close(fh);             printf("_read %s offset %I64u error\n",argv[1],offs);             return;         }         wrv=fwrite(buf,1,rv,fo);         if (wrv!=rv) {             fclose(fo);             _close(fh);             printf("fwrite %s error\n",ofn);             return;         } else {             offs+=rv;             cprintf("%I64u\r",offs);             if (offs>=offs1) break;//         }     }     fclose(fo);     _close(fh);     printf("Copy %s offset %s to %s OK.\n",argv[1],argv[2],ofn); }

user: user-0
content: 你这个代码我怎么看不懂啊？ 比如文件大小为8K，要取出从第6.5K，到7.5K的1K。

user: user-5
content: 从test.pdb读，写入out.dat,  offset有命令参数提供， 如果没有则从头开始读入 #include <stdio.h>  int main(int argc, char*argv[]) {   freopen("test.pdb", "rb", stdin);   freopen("out.dat", "wb", stdout);   unsigned int  offset = 0, length = 1024, rdbytes = 0, total = 0, wtbytes = 0;   if (argc == 2){     sscanf(argv[1], "%u", &offset);   }   char buf[1024];   while (0 == ferror(stdin))   {     rdbytes = fread(buf, 1, 1024, stdin);     total += rdbytes;     if (total >= offset)     {       break;     }   }   if (total > offset){     wtbytes = fwrite(buf + rdbytes - (total - offset), 1, total - offset, stdout);   }   rdbytes = fread(buf, 1, 1024, stdin);   if (rdbytes > 0){     if (rdbytes > length - wtbytes)       rdbytes = length - wtbytes;     wtbytes += fwrite(buf, 1, rdbytes, stdout);   }   return 0; }

user: user-6
content: fopen fread fwrite fclose 就这几个函数，自己查查先。

user: user-5
content: 回复错了， 抱歉。

user: user-5
content: Debug->Exceptions->break when an exception is

user: user-0
content: 用VS2010