0

i am fairly new to C and i have a Problem. Everytime i run the programm i get a "Segmentation fault" and i don't know why. I am pretty sure i know were the problem comes from, so i maked the line in the code. I hope you can help me. Thanks in advance.

struct node {
struct node *next;
struct node *previous;
char data[255];
};

void insert(char *value, struct node **anf, struct node **end)
{
    struct node *var,*temp;
    var=(struct node *)malloc(sizeof(struct node));
    strcpy(var->data, value);
    if(*anf==NULL)
    {
        *anf=var;
        (*anf)->previous=NULL;
        (*anf)->next=NULL;
        *end=*anf;
    }
    else
    {
        temp=var;
        temp->next=NULL;
        temp->previous=*end;
        (*end)->next=temp;
        *end=temp;
    }
}

int einlesen(char *quelldatei, struct node **anf, struct node **end)
{
    FILE *datei;
    if ((datei=fopen(quelldatei,"r")) == NULL)
    {
        return 1;
    }
    else
    {
        char string[255];
        while(fgets (string, 255, datei)!=NULL)
        {
             if (string[strlen(string)-1]=='\n')
             {
             char newstring[strlen(string)-1];
             int i;
                 for(i = 0; i < sizeof(newstring); i++)
             {
                     newstring[i]=string[i];
         }
             newstring[sizeof(newstring)]= '\0';
                 insert(newstring, anf, end);        //NOT WORKING
             }
             else
             {
                 insert(string, anf, end);               //NOT WORKING
             }
         }    
        fclose(datei);
        return 0;
    }
}

int main(int argc, char * argv[])
{
    struct node *anf,*end;
    int a = einlese(##FILENAME##,&anf,&end); //##FILENAME## stands for a local file
    return 0;
}
Stoffl
  • 405
  • 2
  • 4
  • 12

0 Answers0