当前位置: 技术问答>linux和unix
新手,一个简单的程序问题。
来源: 互联网 发布时间:2017-01-20
本文导语: #include FILE *stream,*fp; int filepos=0; int filesize=0; char s[250]; int recvsize=0; int main(viod) { fp=fopen("server","rb"); fseek(fp,0,SEEK_END); filesize=ftell(fp); rewind(fp); while(recvsize != filesize) { fseek(fp,filepos,SEEK_SET); int readsize=fread(s,sizeof(char),2...
#include
FILE *stream,*fp;
int filepos=0;
int filesize=0;
char s[250];
int recvsize=0;
int main(viod)
{
fp=fopen("server","rb");
fseek(fp,0,SEEK_END);
filesize=ftell(fp);
rewind(fp);
while(recvsize != filesize)
{
fseek(fp,filepos,SEEK_SET);
int readsize=fread(s,sizeof(char),250,fp);
filepos=ftell(fp);
recvsize+=readsize;
if((stream=fopen("TEST.c","wb"))==NULL)
{
fprintf(stderr,"cannot open output file.n");
return 1;
}
fwrite(&s,sizeof(char),250,stream);
fclose(stream);
fclose(fp);
}
return 0;
}
出错:
*** glibc detected *** ./write: double free or corruption (top): 0x081c0008 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x6ebc2)[0x68cbc2]
/lib/i386-linux-gnu/libc.so.6(+0x6f862)[0x68d862]
/lib/i386-linux-gnu/libc.so.6(cfree+0x6d)[0x69094d]
/lib/i386-linux-gnu/libc.so.6(fclose+0x154)[0x67c0b4]
./write[0x8048706]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x637113]
./write[0x8048501]
======= Memory map: ========
0061e000-00794000 r-xp 00000000 08:01 394150 /lib/i386-linux-gnu/libc-2.13.so
00794000-00796000 r--p 00176000 08:01 394150 /lib/i386-linux-gnu/libc-2.13.so
00796000-00797000 rw-p 00178000 08:01 394150 /lib/i386-linux-gnu/libc-2.13.so
00797000-0079a000 rw-p 00000000 00:00 0
007ee000-007ef000 r-xp 00000000 00:00 0 [vdso]
00ad7000-00af3000 r-xp 00000000 08:01 394171 /lib/i386-linux-gnu/libgcc_s.so.1
00af3000-00af4000 r--p 0001b000 08:01 394171 /lib/i386-linux-gnu/libgcc_s.so.1
00af4000-00af5000 rw-p 0001c000 08:01 394171 /lib/i386-linux-gnu/libgcc_s.so.1
00fca000-00fe8000 r-xp 00000000 08:01 394137 /lib/i386-linux-gnu/ld-2.13.so
00fe8000-00fe9000 r--p 0001d000 08:01 394137 /lib/i386-linux-gnu/ld-2.13.so
00fe9000-00fea000 rw-p 0001e000 08:01 394137 /lib/i386-linux-gnu/ld-2.13.so
08048000-08049000 r-xp 00000000 08:01 416755 /home/zhayun/write
08049000-0804a000 r--p 00000000 08:01 416755 /home/zhayun/write
0804a000-0804b000 rw-p 00001000 08:01 416755 /home/zhayun/write
081c0000-081e1000 rw-p 00000000 00:00 0 [heap]
b7600000-b7621000 rw-p 00000000 00:00 0
b7621000-b7700000 ---p 00000000 00:00 0
b778c000-b778d000 rw-p 00000000 00:00 0
b779a000-b779e000 rw-p 00000000 00:00 0
bf8be000-bf8df000 rw-p 00000000 00:00 0 [stack]
已放弃
gdb 调试提示:Program received signal SIGABRT, Aborted.
|
while中,去掉:fclose(fp);
关闭文件后,在试图读取,必然会core啊。。。
关闭文件后,在试图读取,必然会core啊。。。