LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-15-2008, 03:17 AM   #1
psernaalvarez
LQ Newbie
 
Registered: Jan 2008
Posts: 6

Rep: Reputation: 0
problem on programming expert system


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>


typedef struct node{
char atributo[20];
char simbolo[20];
char output;
struct node* children;
struct node* next;

} *punt_nodo,nodo;

void leer(char c, FILE* f)
{
char i;
if(i!=EOF) i=fgetc(f);
while ((i!=c)&&(i!=EOF)) i=fgetc(f);
}

punt_nodo creacionnodos (FILE*archivo)
{
punt_nodo nodoaux=(punt_nodo)malloc(sizeof(nodo));
punt_nodo nodoaux2=(punt_nodo)malloc(sizeof(nodo));
char c;
leer('!',archivo);
fscanf(archivo,"%s",nodoaux->simbolo);
leer(':',archivo);
fscanf(archivo,"%s",nodoaux->children->simbolo);
if ((nodoaux->children)!=NULL){
nodoaux2=nodoaux->children;

while((c=fgetc(archivo))!='&'){
leer(':', archivo);
fscanf(archivo, "%s",nodoaux2->next->simbolo);
if ((nodoaux2->next)!=NULL){nodoaux2=nodoaux2->next;}}}

return nodoaux;
}


int main(void)
{FILE* f=fopen("caracteristicas.txt","r");
punt_nodo nodo1=creacionnodos(f);
printf("%s",nodo1->simbolo);
}
 
Old 02-15-2008, 04:57 AM   #2
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 292

Rep: Reputation: 46
Code:
fscanf(archivo,"%s",nodoaux->simbolo);
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.
 
Old 02-15-2008, 06:08 AM   #3
psernaalvarez
LQ Newbie
 
Registered: Jan 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks a lot we have managed to fix the seg error although we still have some problems compiling it,

Thank you
 
Old 02-15-2008, 07:01 AM   #4
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 292

Rep: Reputation: 46
You have some additional (typical) problems with this function:
Code:
void leer(char c, FILE* f)
{
char i;
if(i!=EOF) i=fgetc(f);
while ((i!=c)&&(i!=EOF)) i=fgetc(f);
}
  • 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 */
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
system() calls causing Zombies...!!! expert gothrog Linux - Networking 4 03-17-2005 11:38 AM
Debian xserver problem....any expert?? avols143 Debian 5 07-05-2004 10:31 AM
Why Linux is best Operating System for Learning/Doing System Programming ? ubaid_t General 6 03-21-2004 02:10 PM
Expert ...External Modem Problem fanarr@yahoo.co Linux - Hardware 6 01-01-2003 05:25 PM


All times are GMT -5. The time now is 02:48 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration