I'm creating a linked list,adding the data I've read from file in each node but as I'm trying to print what I hold in nodes my output is some symbols.And even when I cout what I hold in my nodes in ADD function it's ok but as it goes to print function everything goes wrong and I'm pretty sure that my add and print functions work accurately.

class Gnode {
public:
	const char *data;
	Gnode *link;
};

class Glist {
public:
	Gnode *first;
public:
	Glist () {
		first=NULL;
	}
	void Add (const char *d);
	void print ();
};
void Glist::Add (const char *d){
	Gnode *temp,*temp2;
	temp=new Gnode;
	temp->link=NULL;
	temp->data=d;
	if (first==NULL){
		first=temp;
	}
	else{
		temp2=first;
		while (temp2->link!=NULL){
			temp2=temp2->link;
		}
		temp2->link=temp;
	}
}

 void Glist::print( ){
	Gnode *temp1;
	temp1=first;
	do
	{
		if(temp1==NULL)
			cout<<"END"<<endl;
		else
		{
			cout<<temp1->data<<" ";
			temp1=temp1->link;
		}
	}
	while (temp1!=NULL);
 }
int main(){	
	ifstream f("A.txt");
	string s; 
	Glist A;
         getline (f,s,' ');
	const char *p1=s.c_str();
	A.Add(p1);
	A.print();
	getch();
}

You should copy the data pointed to by d inside the Add function, because of this, from www.cplusplus.com on string.c_str():

The returned array points to an internal location with the required storage space for this sequence of characters plus its terminating null-character, but the values in this array should not be modified in the program and are only granted to remain unchanged until the next call to a non-constant member function of the string object.

I don't know if it's what causes the error, but try it out, it is better anyway.

The code worked when i tested it. Printed the characters in ..\Projects\test2\test2\A.txt until it hit space.

You should copy the data pointed to by d inside the Add function, because of this, from www.cplusplus.com on string.c_str():

I don't know if it's what causes the error, but try it out, it is better anyway.

sorry,but I haven't got it.what do you mean by copying the data pointed to by d inside the Add function? do you know any other way for holding strings in linked lists?

The code worked when i tested it. Printed the characters in ..\Projects\test2\test2\A.txt until it hit space.

you mean printing the exact characters not symbols?

I've done it,thanks.

sorry,but I haven't got it.what do you mean by copying the data pointed to by d inside the Add function? do you know any other way for holding strings in linked lists?

I mean that the s.c_str() returns a pointer to the first character in the string. If later, you change or delete the string "s", the pointer you got from the previous call to c_str() is no longer pointing to the right place and the string that is "held" by your node will be lost or corrupted. This is why I say you should copy the data pointed to by d inside the Add function. What you need to do is inspect the size of the string, allocated enough memory for it, and copy the content of the string (the list of characters) to the new memory (I case you thought that was automatic, well it's NOT). This way your node is holding it's OWN copy of the string and there is no danger that another part of the software might temper with it, this is part of the idea of encapsulation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.