ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hello we are trying to crate a small expert system for a assignment for our university and the part which creates nodes using rete by reading characteristics from a file gives us a core dumped, we have problems findind where the problem is because we are new to all of this programming in C, if anyone could help we would be very grateful.
Thanks a lot
code:
# include <string.h>
#include <stdio.h>
#include <stdlib.h>
Here you trust the input data to not overflow simbolo. Very dangerous, easily breakable.
At least specify the field width like so: "%19s".
Code:
fscanf(archivo,"%s",nodoaux->children->simbolo);
Now you use nodoaux->children which has never been inititlaized. Which means fscanf will attempt to write to some random memory address. Hence the segfault.
i and c should be int, not char to avoid signed/unsigned problems and to be able to match EOF. (Note that fgetc() returns int, not char.)
The first if() doesn't make sense, as i has never been initialized. In C, you first read the character, then check if it was EOF.
The variable naming is unusual. Not an error, but makes code more difficult to read. I'd suggest to use c instead of i and to use a more descriptive name instead of c.
You might want to return a value to indicate if the function terminated successfully or because it encountered EOF.
So all in all you'd come up with something like:
Code:
int leer(int delim, FILE *f)
{
int c;
while (EOF != (c = fgetc(f))) {
if (c == delim)
return 1;
}
return 0; /* EOF */
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.