Reputation: 2339
This my program where it takes 10 integers and finds out if even or odd and stores them in respective files.I use gcc.
#include<stdio.h>
int main()
{
int a,i;
FILE *fp1,*fp2,*fp3;
fp1=fopen("data","w");
for(i=0;i<10;i++)
{
scanf("%d",&a);
putw(a,fp1);
}
fclose(fp1);
fp1=fopen("data","r");
fp2=fopen("even","w");
fp3=fopen("odd","w");
while((a=getw(fp1))!=EOF)
if(a%2==0)
putw(a,fp2);
else
putw(a,fp3);
fclose(fp1);
fclose(fp2);
fclose(fp1);
return 0;
}
I get this after entering the 10 values
*** glibc detected *** ./a.out: double free or corruption (!prev): 0x09b79008 ***
======= Backtrace: =========
/lib/libc.so.6(+0x6c501)[0x17c501]
/lib/libc.so.6(+0x6dd70)[0x17dd70]
/lib/libc.so.6(cfree+0x6d)[0x180e5d]
/lib/libc.so.6(fclose+0x14a)[0x16c81a]
./a.out[0x80485e4]
/lib/libc.so.6(__libc_start_main+0xe7)[0x126ce7]
./a.out[0x8048421]
======= Memory map: ========
00110000-00267000 r-xp 00000000 08:04 686829 /lib/libc-2.12.1.so
00267000-00268000 ---p 00157000 08:04 686829 /lib/libc-2.12.1.so
00268000-0026a000 r--p 00157000 08:04 686829 /lib/libc-2.12.1.so
0026a000-0026b000 rw-p 00159000 08:04 686829 /lib/libc-2.12.1.so
0026b000-0026e000 rw-p 00000000 00:00 0
007b2000-007ce000 r-xp 00000000 08:04 686805 /lib/ld-2.12.1.so
007ce000-007cf000 r--p 0001b000 08:04 686805 /lib/ld-2.12.1.so
007cf000-007d0000 rw-p 0001c000 08:04 686805 /lib/ld-2.12.1.so
009cc000-009cd000 r-xp 00000000 00:00 0 [vdso]
00a2c000-00a46000 r-xp 00000000 08:04 686863 /lib/libgcc_s.so.1
00a46000-00a47000 r--p 00019000 08:04 686863 /lib/libgcc_s.so.1
00a47000-00a48000 rw-p 0001a000 08:04 686863 /lib/libgcc_s.so.1
08048000-08049000 r-xp 00000000 08:04 753598 /home/bobby/Desktop/c/a.out
08049000-0804a000 r--p 00000000 08:04 753598 /home/bobby/Desktop/c/a.out
0804a000-0804b000 rw-p 00001000 08:04 753598 /home/bobby/Desktop/c/a.out
09b79000-09b9a000 rw-p 00000000 00:00 0 [heap]
b7600000-b7621000 rw-p 00000000 00:00 0
b7621000-b7700000 ---p 00000000 00:00 0
b77df000-b77e0000 rw-p 00000000 00:00 0
b77ea000-b77eb000 rw-p 00000000 00:00 0
b77ed000-b77f0000 rw-p 00000000 00:00 0
bfed2000-bfef3000 rw-p 00000000 00:00 0 [stack]
Aborted
I want to know what is it.This is first time i got a error of this kind.I want to understand the error and find out what is wrong with my program.
Upvotes: 2
Views: 4604
Reputation: 22149
You are closing fp1
twice. Look at the last line before return :)
I suspect you want to close fp3
instead, if I've interpreted your program correctly. There's also a clue in the stacktrace, the error happens somewhere inside fclose
.
Upvotes: 4