Hello all,

I am in need of some assistence. So if anyone could help that would make my day;).
This is a piece of my code

for( int i = 0; i <= 13; i++ )
		 {
		 cout << "SLOT : " << slotNumber;	
		 fprintf( HashTable, "\t%i slot : "  , slotNumber );
			for( n = Head; n != NULL; n = n->Next )
			 {
				 if ( hashfunction(n->data) == slotNumber )
				 {
				 cout  << " " << n->data << " , ";
						
				 fprintf( HashTable, "\t%s "  , n->data);

As you can see , I'm trying to output my n->data to a (text)file named hashtable
unfortunately all I'm getting is

slot 0:
slot 1: (null)
slot 2:
slot 3 (null) (null)

And stuff like that. I want it to output the real data stored in my hashelement. The cout works fine so I know there is data there. Is there anyone with a solution in fprintf or perhaps another function that could write my actual data correct to my (text) file.

ps. It's necessary for me to output the data as I've written above, so that my text output could resemble something like
slot 0: element1 , element2
slot 1: element5 ,
slot 2:
slot 3: element6, element8

If anyone has an answer that would be super.
thanks

Why not use an ofstream to write to the file instead of fprintf and FILE pointers? If cout works just fprintf doesn't, it probably means that %s isn't the right specifier for n->data, but if you use an ofstream for the file output that's not a problem anymore because it infers the type just like cout.

If you still want to use fprintf, how is n->data declared?

Thanks a lot for your advice and suggestion.
I tried every specifier possible for fprintf but no result.
I liked your solution , as in to try the ofstream unfortunately it can only put one character in each time with the put or write function. You asked for the n->data declaration, it's a string, so that gives me some problems I'm afraid.

>I tried every specifier possible for fprintf but no result.
Ed's guess is that n->data is an std::string object, and this would solve the problem:

fprintf( HashTable, "\t%s "  , n->data.c_str());

But that's only a guess. ;)

>I liked your solution , as in to try the ofstream unfortunately it can only put one character in each time with the put or write function.
Is this a limitation of your compiler? Because the << operator is overloaded for ofstream too. You can do the following as long as fout is defined and open:

cout << " " << n->data << " , ";

// Does the same thing as above, except to a file
fout << " " << n->data << " , ";

>You asked for the n->data declaration, it's a string
What kind of string? In C++ you can have one of two standard strings: the std::string class and an array of char where the last character is '\0'. You can also define your own string classes, so just saying that you're using a string isn't as informative as it used to be. ;)

Ed excellent;)

fprintf( HashTable, "\t%s "  , n->data.c_str());

This one works like charm.

It's from the string class btw ( answer to your other question ).
Thanks man.

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.