-1

So for my school program, I simply have to read in a listing of integers from a text file that is in the format below, with the first line indicating the amount of integers. Then sort the integers(they are unsorted in the txt file), and then find the mean. I'm stuck just trying to read in the integers into an array though from the test .txt files I've been using.

9

1

3

2

4

...

#include<stdio.h>
#include<stdlib.h>

int amtValues;
int *values;

float find_median (int* values){
    if(amtValues%2==0){
        return ((values[amtValues/2] + values[amtValues/2 -1]) /2.0);
    }
    else
        return values[amtValues/2];
}

int main(int argc, char **argv){

//open the file, copy the data from the file, and determine the number of values
    //open the filestream
    FILE* fp = fopen(argv[1], "r");
    if(!fp){
        printf("error reading file\n");
        return 1;
    }

    //determine the number of values
    char *lineOne;
    
    if(fgets(lineOne, 80, fp)!=1)
        puts (lineOne);
    lineOne++;
    amtValues = atoi(lineOne);
    printf("\nThe amount of values is: %d\n",amtValues);

    /*allocate memory for values array, and copy the values from file.*/
    values = (int*)malloc(amtValues*sizeof(int));
    int i=0;

    while(!feof(fp)){
        int curNum;
        fscanf(fp, "%d", &curNum);
        values[i] = curNum;
        i++;
    }
    fclose(fp);

    for(i=0; i<amtValues; i++)
        printf("\n%d");
}

This is what I have going at the moment, I get an error saying segmentationFault(core Dumped). I'm new to C so I'm really not sure what that means.

Community
  • 1
  • 1
skulltula
  • 153
  • 1
  • 3
  • 10
  • Which line is causing the issue ? Which line is shown by the compiler ? – Hearner Apr 06 '18 at 06:46
  • Possible duplicate of [Segmentation fault in fgets() - C language](https://stackoverflow.com/questions/19390936/segmentation-fault-in-fgets-c-language) – Afshin Apr 06 '18 at 06:51
  • 1
    terrible code. Partially relies on arguments, mainly on globals. – Jacek Cz Apr 06 '18 at 07:02
  • 1
    Also please see [why `while(!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). – unwind Apr 06 '18 at 07:41

2 Answers2

1

I have not checked your whole code, but here cause segmentation fault:

//determine the number of values
char *lineOne;

if(fgets(lineOne, 80, fp)!=1)
    puts (lineOne);

You need to have memory for lineOne. Only a pointer does not mean you have memory for your buffer and you need to allocate it on heap or stack. Try it like this:

char lineOne[81]; // allocation on stack

or

char *lineOne = malloc(sizeof(*lineone) * 81); // allocate on heap
Afshin
  • 8,839
  • 1
  • 18
  • 53
1

I get an error saying: segmentation fault (core dumped)

A segmentation fault is practically some undefined behavior (UB), and means that your program is buggy, because the computer is dereferencing some invalid memory address (outside of the virtual address space of your process).

(I am guessing you are on Linux or some other POSIX system, but that should be mentioned in your question)

A core dump (on Linux, see core(5) for details) is a file -generally named core- describing the state of the faulty process (when the fault happens, see signal(7)), notably its virtual address space. You can use the gdb debugger to debug post-mortem a core dump. You could also disable core dumps (but you better not, they are very useful), e.g. with ulimit -c bash builtin (or setrlimit(2) system call).

However, not all undefined behaviors give such a fault. You are lucky to get one. And it could happen (e.g. because of ASLR) that another run of the same buggy executable won't core dump or segfault (but would have some even worse behavior).

In practice:

  • compile with all warnings and debug info : gcc -Wall -Wextra -g with GCC. Improve your code to get no warnings at all (in your case, you would get some warnings with your code).

  • read the documentation, notably of your compiler, and of every function you are using (e.g. you are using fgets wrongly: it will never give 1). Later, you could even read the C11 standard n1570.

  • learn how to use the gdb debugger to understand the behavior of your faulty program. This is an essential skill to acquire. You should have tests.

  • learn more about UB, and be very scared of them. The worst UB is the one that apparently don't (always) give a fault. Tools like valgrind are helpful.

Maybe you want to use getline(3). See this. Maybe you could have a flexible array member, like here and reason with some abstract data type.


Be also aware of the Halting Problem and its undecidability.

PS. all developers make bugs. Finding them is challenging and could be fun. But programming is difficult.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • there is an obvious source for his segmentation fault. `lineOne` is not allocated as I mentioned in my reply. – Afshin Apr 06 '18 at 06:53
  • Yes, but OP needs to learn how to debug his programs. If we do all the debugging, he will never learn – Basile Starynkevitch Apr 06 '18 at 06:53
  • just a suggestion for edit if you think I'm right. I think it is better to mention that segmentation fault is normally caused by invalid access memory(at least from what I have experienced). So it is important to check for invalid memory access first, such as pointers. – Afshin Apr 06 '18 at 06:58
  • I would say that it is better to ensure that pointers are valid when dereferenced. How to do that is left as an exercise to the reader. :-) :-) – Basile Starynkevitch Apr 06 '18 at 07:07