0

I'm trying to read a file to extract numbers and then compare it to find the most used number.

Every time I run this, I get this msg in the debbuger (obviusly the program doesn't run).

Program received signal SIGSEGV, Segmentation fault.
In ?? () ()

I can't find the problem in my code, I deleted my file and the program runs, but when I put the "test" file this thing just crash...

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

int main(){
    FILE *fp;
    int especie = 0;
    int es = 0;
    int noes = 0;
    int i = 0;

    fp = fopen("especies.txt", "r");

    if (fp == NULL){
        printf("\nError de apertura del archivo. \n\n");
    }
    else{
        for (i; i <= 10000000000; i++){
            while (feof(fp) == 0){
                fscanf(fp, "%d", &especie);

                if(i == especie){
                    es++;
                }
                else{
                    noes++;
                }
            }

            if (es >= ((es+noes)/2)){
                break;
            }
            else{
                rewind(fp);
                es = 0;
                noes = 0;
            }

        }

        printf("%d", i);
        fclose(fp);
    }

    return 0;
}
  • 4
    First of all, don't do `while (!feof(...))`, it will in most cases not work as expected. In your case instead do `while (fscanf(...) == 1)`. Then to find out *where* the crash happens, run in a debugger. If you still can't figure it out, please update the question to show us where in your code the crash happens, and the values of involved variables. – Some programmer dude Oct 02 '15 at 06:11
  • 1
    How this code can compile successfully? I am noticing some missing parentheses. – rootkea Oct 02 '15 at 06:12
  • 1
    Compile the program with debugging information, e.g. `-g`. Then you might see a line number instead of question marks. Or run the program using a debugger. – Olaf Dietsche Oct 02 '15 at 06:12
  • First compile the program with the debug flag, the run gdb and then after your program crashes do a backtrace to find out where exactly it is crashing. – shafeen Oct 02 '15 at 06:13
  • 4
    Please see [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/q/5431941/2173917) – Sourav Ghosh Oct 02 '15 at 06:15
  • The problem was Windows, in Ubuntu this doesn't give me any problems. Thanks for your help guys :D – Arturo Lennon Oct 02 '15 at 06:35
  • Since the program you have shown has syntax errors, and therefore can't be compiled, _it does not seem to be the program you are actually running_. It is hard to give any advice when you are not showing us the right program. – Thomas Padron-McCarthy Oct 02 '15 at 06:36
  • 4
    `10000000000` seems awfully large for an `int`, and that entire "design" is very dubious. If you're doing a loop until EOF, why do you also need to iterate an `i`? – unwind Oct 02 '15 at 06:48
  • I need to check the most used number in the file between 0 and 1000000000, at this moment (with the updated code) is working, I can't figure how to do it faster. – Arturo Lennon Oct 02 '15 at 06:55

0 Answers0