I've been for a while trying to find out why i'm getting segmentation fault on this code. It's supposed to get some 11-digit numbers from a file, find out which ones start with the same 4 numbers and list them on screen.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CHOP_SIZE 5
#define INIT_CHOP 100
typedef enum{
OK,
ERROR_MEMORY,
ERROR_NULL_POINTER,
} state_t;
state_t list(FILE * file,int number){
int *v, *aux;
size_t used_size;
size_t alloc_size, i;
for(used_size=0;fscanf(file,"%d",&v[used_size])==EOF;used_size++){
if(used_size==0){
if((v=(int*)malloc(sizeof(int)*INIT_CHOP))==NULL)return ERROR_MEMORY;
alloc_size=1;
}
if(used_size==alloc_size){
if((aux=(int*)realloc(v,sizeof(int)*(alloc_size+CHOP_SIZE)))==NULL){
free(v);
v=NULL;
return ERROR_MEMORY;
}
alloc_size+=CHOP_SIZE;
v=aux;
}
}
for(i=0;i<used_size;i++){
if(v[i]/10000000==number){
printf("%d\n",v[i]);
}
}
return OK;
}
int main(void){
FILE *file;
state_t state;
file=fopen("file1","rb");
state=list(file, 1111);
return EXIT_SUCCESS;
}