I had been trying to read input from a file, but it seems that something doesn't work correctly...Instead of reading the word "Words" that exists in the text,the printf is always showing 2 additional random characters not included in the file...
The function is:
void search(struct word *w,FILE *f){
char *c;
char c2;
int i,j,k,l;
c=(char*)malloc(120*sizeof(char));
i=1;
while(f!=NULL) {
c2=fgetc(f);
while(c2!=EOF) {
while(c2!='\n') {
k=0;
while(c2!=' ') {
*(c+k)=c2;
k=k+1;
c2=getc(f);
}
if(w->name==c)
insert(i,j+1,name,&w);
}
memset(c, 0, sizeof(c));
j=j+k+1;
}
i=i+1;
}
}
}
the main function is
int main()
{
struct word *s;
s=(struct word*)malloc(sizeof(struct word));
s->name=(char*)malloc(20*sizeof(char));
s->result=NULL;
scanf("%s",s->name);
search(s);
printres(s);
system("pause");
exit(0);
}
and the structs are
struct position
{
char *filename;
int line;
int place;
struct position *next;
};
struct word
{
char *name;
struct word *right;
struct word *left;
struct position *result;
};
Why do these additional 2 characters appear? What should I do?
fflush. And you should compile with all warnings & debug info (e.g., if using GCC, withgcc -Wall -g) and use the debugger. BTW, your question is probably operating system specific (perhaps related to end of file markers). And your code is awfully indented. Please edit your question to improve the indentation & formatting.