hi :) i managed to compile it but when i try it - it scream Segmentation-Fault.
i have no idea where it happens exactly . only know that it happen :(
can someone help me ?
thanks in advance
the program try to implement some kind of hash table :
index - [ in which file the index appear and the number of appearance at each file)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
# define RANGE 29
typedef struct file_numbers{
char file_name[10];
int count;
struct file_numbers *next;
} node;
void prints (node table[]);
void build_list(FILE *files,char* string,node table[]);
int main(int argc,char* argv[])
{
node table[RANGE];
FILE *files;
if (argc==1)
{
printf("Error - no arguments were entered");
return 1;
}
while(--argc>0)
{
if((files=fopen(*++argv,"r"))==NULL)
{
printf("Cannot open file");
return 1;
}
else
build_list(files,*argv,table);
}
prints(table);
return 0;
}
void prints(node table[])
{
int index;
node* pos;
for(index=0;index<RANGE;index++)
{
pos=table[index].next;
if(!(pos==NULL))
{
printf("%d appears in file %s %d times" ,index,pos->file_name,pos->count);
while(!((pos->next)==NULL))
{
pos=pos->next;
printf(", in file %s %d",pos->file_name,pos->count);
}
}
}
}
void build_list(FILE *files,char* string,node table[])
{
int index=0;
node* pos;
node* new_node ;
index=fgetc(files);
while(!feof(files))
{
if(index>=0 && index<RANGE){
if(table[index].next==NULL)
{
new_node=(node*)malloc(sizeof(node));
strcpy(new_node->file_name,string);
(new_node->count)++;
table[index].next=new_node;
}
pos=table[index].next;
/*finding a node with the file name"*/
while(!strcmp(pos->file_name,string))
{
pos=pos->next;
}
/* this number already appear*/
if(strcmp(pos->file_name,string))
{
(*pos).count++;
}
/* create a new node*/
else
{
new_node=(node*)malloc(sizeof(node));
strcpy(new_node->file_name,string);
(new_node->count)++;
(*pos).next=new_node;
}
}
index=fgetc(files);
}
}