Pregunta

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

¿Fue útil?

Solución

The printf("%n", numberOfIntegers) call will write a zero into the integer pointed to by numberOfIntegers. That's because there are zero characters written before the %n format is interpreted (see the specification for printf()).

Probably, you wanted to use:

printf("%d\n", *numberOfIntegers);

An SSCCE

Here is an SSCCE (Short, Self-Contained, Correct Example) closely based on the code shown in the question that demonstrates that there is nothing horribly wrong with that code — and hence that the problems encountered in the question are in code that is not shown.

#include <stdio.h>

static
int *readIntegers(const char *filename, int *numberOfIntegers)
{
    FILE *fp = fopen(filename, "r");
    int count;
    if (fp == NULL)
    {
        return NULL;
    }
    while (fscanf(fp, "%d", &count) > 0)
    {
        (*numberOfIntegers)++;
    }
    printf("%d\n", *numberOfIntegers);
    fclose(fp);
    return numberOfIntegers;
}

int main(int argc, char **argv)
{
    const char *file = "data";
    int num = 0;
    int *p;
    if (argc > 1)
        file = argv[1];
    p = readIntegers(file, &num);
    if (p != &num)
    {
        fprintf(stderr, "Failed to read numbers from file %s\n", file);
        return 1;
    }
    else
        printf("Number of numbers in file %s is %d\n", file, num);
    return 0;
}

Given a file called data containing:

0 1 2 3 4 5 6 7 8 9 10

Or the equivalent file with each blank replaced by a newline, the output of the program is:

11
Number of numbers in file data is 11

If the program is called ri (Read Integers), and is called as:

$ ./ri missing
Failed to read numbers from file missing
$

In neither case does it crash. I added the code to close the file and return the input pointer. This code compiles cleanly on Mac OS X 10.8.4 using GCC 4.8.1 with the command line set to extremely stringent:

$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
>     -Wold-style-definition ri.c -o ri
$

So, as stated, the problem is not directly in the code fragment presented to us for scrutiny.

The compiler warnings that you are ignoring really worry me precisely because they point to problems that will manifest themselves after the code shown successfully executes. Returning a pointer to a local variable is a way to crash programs; returning the wrong type of pointer from a function is a way to crash programs. (I'll grant you that the unused variable warning is about as benign as they get — I am not worried about that one, though by commenting out the declaration, you've prevented it from appearing.)

So, you need to present more program for anyone to diagnose successfully your problem. What you show is OK code.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top