Tuesday, September 10, 2013

Read Integers and count them, then rewind---C

Read Integers and count them, then rewind---C

this is a small piece of my homework that I am having difficulty with.
There is more to the function then this part but this is all that I am
having difficulty with(There is more after). When I run this, it tells me
that it failed to read the integer file. Can anyone tell me where I should
start looking on where I went wrong?
int * readIntegers(const char * filename, int * numberOfIntegers)
{
FILE* fp = fopen(filename, "r");
int count = 0;
int array;
if(fp== NULL)
{
return NULL;
}
while(fscanf(fp, "%d", &count)>0)
{
(*numberOfIntegers)++;
}
printf("%d\n",*numberOfIntegers);
Compiler warnings, I am not concerned about these since they don't have to
do with that I posted
warning: return from incompatible pointer type [enabled by default]
warning: function returns address of local variable [enabled by default]
warning: unused variable 'array' [-Wunused-variable]
And when it runs this is what I get...
./pa03 inputs/input0 > outputs/output0 ./pa03 inputs/input0 >
outputs/output0
Failed to read integer-file 'inputs/input0', aborting
cat outputs/output0
Failed to read integer-file 'inputs/input0', aborting
cat outputs/output0
Also, the print statement towards the end was diagnostic, later in the
function I will return an array that will display the answer. I was trying
to see if the numberOfInputs got updated to the correct number, which has
not happened so far. I will update my print statemnet to see if it
works...will update.
Here is the entire log now.
gcc -g -Wall -Wshadow -c -o obj/answer03.o answer03.c answer03.c: In
function 'readIntegers': answer03.c:85:2: warning: return from
incompatible pointer type [enabled by default] answer03.c:85:2: warning:
function returns address of local variable [enabled by default]
answer03.c:68:6: warning: unused variable 'array' [-Wunused-variable] gcc
-g -Wall -Wshadow -c -o obj/pa03.o pa03.c gcc -g -Wall -Wshadow
obj/answer03.o obj/pa03.o -o pa03
./pa03 inputs/input0 > outputs/output0 * glibc detected * ./pa03:
munmap_chunk(): invalid pointer: 0x00007fff9ffebad0 * ======= Backtrace:
========= /lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x2acd35b25b96]
./pa03[0x400c67]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x2acd35ac876d]
./pa03[0x400699] ======= Memory map: ======== 00400000-00402000 r-xp
00000000 08:06 1443970
/home/matt/ECE264/solutions/PA03/pa03 00601000-00602000 r--p 00001000
08:06 1443970
/home/matt/ECE264/solutions/PA03/pa03 00602000-00603000 rw-p 00002000
08:06 1443970
/home/matt/ECE264/solutions/PA03/pa03 0219f000-021c0000 rw-p 00000000
00:00 0 [heap] 2acd35882000-2acd358a4000 r-xp 00000000 08:06 8130309
/lib/x86_64-linux-gnu/ld-2.15.so 2acd358a4000-2acd358a9000 rw-p 00000000
00:00 0 2acd35aa4000-2acd35aa5000 r--p 00022000 08:06 8130309
/lib/x86_64-linux-gnu/ld-2.15.so 2acd35aa5000-2acd35aa7000 rw-p 00023000
08:06 8130309
/lib/x86_64-linux-gnu/ld-2.15.so 2acd35aa7000-2acd35c5c000 r-xp 00000000
08:06 8127294
/lib/x86_64-linux-gnu/libc-2.15.so 2acd35c5c000-2acd35e5b000 ---p 001b5000
08:06 8127294
/lib/x86_64-linux-gnu/libc-2.15.so 2acd35e5b000-2acd35e5f000 r--p 001b4000
08:06 8127294
/lib/x86_64-linux-gnu/libc-2.15.so 2acd35e5f000-2acd35e61000 rw-p 001b8000
08:06 8127294
/lib/x86_64-linux-gnu/libc-2.15.so 2acd35e61000-2acd35e68000 rw-p 00000000
00:00 0 2acd35e68000-2acd35e7d000 r-xp 00000000 08:06 8130119
/lib/x86_64-linux-gnu/libgcc_s.so.1 2acd35e7d000-2acd3607c000 ---p
00015000 08:06 8130119
/lib/x86_64-linux-gnu/libgcc_s.so.1 2acd3607c000-2acd3607d000 r--p
00014000 08:06 8130119
/lib/x86_64-linux-gnu/libgcc_s.so.1 2acd3607d000-2acd3607e000 rw-p
00015000 08:06 8130119
/lib/x86_64-linux-gnu/libgcc_s.so.1 7fff9ffcd000-7fff9ffee000 rw-p
00000000 00:00 0 [stack] 7fff9ffff000-7fffa0000000 r-xp 00000000 00:00 0
[vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
[vsyscall] ./bin/test.sh: line 20: 8797 Aborted (core dumped) ./$EXEC $INF
> $OUTF cat outputs/output0
5 Unsorted array: 35254288 0 194 5 -1610695872 Sorted array: 35254289 5
-1610695984 1 -1610695872 -- FAIL (not sorted). Integer '35254288' not
found -- FAIL. Integer '35254289' not found -- FAIL.
Checking expected output: expected/expected0 - FAIL. Expected:
Unsorted array: 4 1 3 2 5 Sorted array: 1 2 3 4 5 -- correct. Integer '4'
in the 3th position in the sorted array -- pass. Integer '5' in the 4th
position in the sorted array -- pass.
make: * [test0] Error 1
Test case: 4 1 3 2 5

No comments:

Post a Comment