i have this program that should print first name, middle name and last name. it almost works, but when I print strings, weird characters appear. I don't know how it's called, but instead of strings appearing, those characters appear. does anybody know why? I included parts of my program here:

typedef struct nodetag0{

	char *fname;
	char *mname;
	char *lname;
}name;

typedef struct nodetag{
	name mName;
}node;
main(){
node *root = NULL;
node tempo = (node*) malloc (sizeof(node));

fn = strtok(NULL, tmp);//taken from file
tempo->mName.fname = fn;

root = tempo;
printf("fname: %s",root->mName.fname);
}

I get the error, segmentation fault. I guess it's because of the printing?

weird characters appear. I don't know how it's called

It's called garbage value.

fn = strtok(NULL, tmp);//taken from file
tempo->mName.fname = fn;

Are you successful in retrieving the values from file into tmp?

there's something missing in the code I posted.

temp = fgets(str2, 200, fp);

tmp is an array which contains the delimiter for strtok();

I can succesfully retrieve the values from the file because i can print the other data which are integers.

wheres your first call to strtok()? The correct way to use strtok is here.

Edited 6 Years Ago by nbaztec: n/a

temp = fgets(str2, 200, fp);
if(temp!=NULL);
en = strtok(str2, tmp);

here's the first call of strtok();

node tempo = (node*) malloc (sizeof(node));

Change to:

node [B]*[/B]tempo = (node*) malloc (sizeof(node));

Edited 6 Years Ago by nbaztec: n/a

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct nodetag0{

	char *fname;
	char *mname;
	char *lname;
}name;

typedef struct nodetag1{

	long month;
	long date;
	long year;
}birthday;

typedef struct nodetag2{
	long hireMonth;
	long hireDate;
	long hireYear;
}hireDay;

typedef struct nodetag{
	long employeeNumber;
	long em;
	name mName;
	birthday bday;
	char *address;
	char *rank;
	long salary;
	hireDay hdate;

	struct nodetag *parent;
	struct nodetag *left;
	struct nodetag *right;
		
}node;

main(){

	FILE *fp, *fpn;
	char str2[100];
	int ctr = 0;	
	int m = 0;
	node *root = NULL;
	int x = 0;
	int choice = 0;

	node *tempo = (node*) malloc (sizeof(node));
	long r;
	char* end;
	char *en;//storage when tokenizing
	char *fn;
	char *mn;
	char *ln;
	char *bm;
	char *bd;
	char *by;
	char *ad;
	char *rk;
	char *sl;
	char *hm;
	char *hd;
	char *hy;
	char tmp[] = "|";//array for the delimiters
	char *temp;
	long a,b,c,d,e,f,g,h;

	fp = fopen("textfile.txt", "r");
		while(!feof(fp)){
			fscanf(fp,"%c",&str2[ctr]);
			if(str2[ctr] == '+'){
				m++;
			}
		}
		
		printf("end line: %d\n", m);
		fclose(fp);
		printf("a:%d\n", m);

		fp = fopen("textfile.txt", "r");
				while(x != m){
					temp = fgets(str2, 200, fp);
					if(temp!=NULL);
					en = strtok(str2, tmp);
					r = strtol(en,&end,10);
						tempo->em = r;
						tempo->employeeNumber = r;
					fn = strtok(NULL, tmp);
						tempo->mName.fname = fn;
					mn = strtok(NULL, tmp);
						tempo -> mName.mname = mn;
					ln = strtok(NULL, tmp);
						tempo -> mName.lname = ln;
					by = strtok(NULL, tmp);
					b = strtol(by,&end,10);
						tempo -> bday.year= b;
					bm = strtok(NULL, tmp);
					c = strtol(bm,&end,10);
						tempo -> bday.month= c;
					bd = strtok(NULL, tmp);
					d = strtol(bd,&end,10);
						tempo -> bday.date= d;
					ad = strtok(NULL, tmp);
						tempo->address = ad;
					rk = strtok(NULL, tmp);
						tempo->rank = rk;
					sl = strtok(NULL, tmp);
					e = strtol(sl,&end,10);
						tempo->salary = e;
					hy = strtok(NULL, tmp);
					f = strtol(hy,&end,10);
						tempo-> hdate.hireYear = f;
					hm = strtok(NULL, tmp);
					g = strtol(hm,&end,10);
						tempo-> hdate.hireMonth = g;
					hd = strtok(NULL, tmp);
					h = strtol(hd,&end,10);
						tempo-> hdate.hireDate = h;
				}	
				

					//load bst.. iisaisahin dito  by line.
		
		
	fclose(fp);
		root = tempo;

		printf("fname: %s",root->mName.fname);
}

here's my complete code. thank you very much!

char tmp[] = "|";//array for the delimiters

This limits tmp to store only 1 character excluding NULL. Hope this is intentional.

if(temp!=NULL);

The semi-colon terminates the if-condition straight away.

Which part the error creeps in you can try putting cin.get() after each to test after which the error occurs ( or by breakpointing). Can you show some Sample Output?

I just realized that I did not increment the x variable that's why the program won't stop scanning the textfile. :) sorry.

This article has been dead for over six months. Start a new discussion instead.