P
picknicker187
hi,
the program below is supposed to read data out of a txt file into an array
and the second part of the data to a linked list.
8 is the number of words, the 10 terms below represent relations between
the words, but this is not important.
the txt file looks like this:
8
Auto
Fahrzeug
Maschine
Boot
Nachen
Kraftfahrzeug
Blechkiste
Gegenstand
the problem is in the last part of the program, the linked list. the
program compiles good, but everytime i run it, it makes a bus error. i´ve
tried so many things, but it just keeps doing it. maybe there´s a logic
mistake in the list or something i can´t see and someone else will see
right up. please help me with this one, i need this program to run as soon
as possible.
thanks!!!!
bastian
/*reads words to an array and to a linked list*/
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int x, y;
typedef struct relations {
char relType;
char *word;
char *dest;
struct relations *next;
}Relations;
int main(void)
{
FILE *fp;
char buf, word[80], dest[80];
Relations *neu = NULL;
Relations *head = NULL;
Relations *aktuell = head;
if( (fp = fopen("eingabe.txt", "r")) == NULL)
{
fprintf(stderr, ("Fehler\n"));
exit(1);
}
fscanf(fp, "%d", &x);
char words[x][20];
for(y=0; y < x; y++)
{
fscanf(fp, "%s", words[y]);
}
for(y=0; y < x; y++)
{
printf("%s\n", words[y]);
}
fscanf(fp, "%c", &buf);
fscanf(fp, ">%c %s %s\n", &buf, word, dest);
neu=(Relations*)malloc(sizeof(Relations));
neu->next=head;
head = neu;
neu->relType = buf;
strcpy(neu->word, word);
strcpy(neu->dest, dest);
aktuell=head;
while(!feof(fp)){
fscanf(fp, ">%c %s %s\n", &buf, word, dest);
while (aktuell -> next) aktuell = aktuell -> next;
neu = (Relations*) malloc(sizeof(Relations));
aktuell -> next = neu;
neu -> next = NULL;
neu->relType = buf;
strcpy(neu->word, word);
strcpy(neu->dest, dest);
printf("%c", buf);
}
fclose(fp);
return 0;
}n
the program below is supposed to read data out of a txt file into an array
and the second part of the data to a linked list.
8 is the number of words, the 10 terms below represent relations between
the words, but this is not important.
the txt file looks like this:
8
Auto
Fahrzeug
Maschine
Boot
Nachen
Kraftfahrzeug
Blechkiste
Gegenstand
H Fahrzeug Auto
H Fahrzeug Boot
S Auto Kraftfahrzeug
S Auto Blechkiste
S Boot Nachen
S Nachen Boot
H Maschine Auto
Y Auto Fahrzeug
Y Boot Fahrzeug
Y Fahrzeug Gegenstand
the problem is in the last part of the program, the linked list. the
program compiles good, but everytime i run it, it makes a bus error. i´ve
tried so many things, but it just keeps doing it. maybe there´s a logic
mistake in the list or something i can´t see and someone else will see
right up. please help me with this one, i need this program to run as soon
as possible.
thanks!!!!
bastian
/*reads words to an array and to a linked list*/
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int x, y;
typedef struct relations {
char relType;
char *word;
char *dest;
struct relations *next;
}Relations;
int main(void)
{
FILE *fp;
char buf, word[80], dest[80];
Relations *neu = NULL;
Relations *head = NULL;
Relations *aktuell = head;
if( (fp = fopen("eingabe.txt", "r")) == NULL)
{
fprintf(stderr, ("Fehler\n"));
exit(1);
}
fscanf(fp, "%d", &x);
char words[x][20];
for(y=0; y < x; y++)
{
fscanf(fp, "%s", words[y]);
}
for(y=0; y < x; y++)
{
printf("%s\n", words[y]);
}
fscanf(fp, "%c", &buf);
fscanf(fp, ">%c %s %s\n", &buf, word, dest);
neu=(Relations*)malloc(sizeof(Relations));
neu->next=head;
head = neu;
neu->relType = buf;
strcpy(neu->word, word);
strcpy(neu->dest, dest);
aktuell=head;
while(!feof(fp)){
fscanf(fp, ">%c %s %s\n", &buf, word, dest);
while (aktuell -> next) aktuell = aktuell -> next;
neu = (Relations*) malloc(sizeof(Relations));
aktuell -> next = neu;
neu -> next = NULL;
neu->relType = buf;
strcpy(neu->word, word);
strcpy(neu->dest, dest);
printf("%c", buf);
}
fclose(fp);
return 0;
}n