Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

cleves

pointers

This topic is 5232 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I''ve got a pointer that points to a string of text.How can I output this string to txt file? Everytime I try to write it to file it showes me the adress and not the value. I know it''s noob question, but i wasn''t programming for a very long time. Thanks a lot.

Share this post


Link to post
Share on other sites
Advertisement
Does this sample program help?


#include <iostream>
using namespace std;

int main()
{
char* text = "Here is My Text\n";

cout << text << endl; //Display the Text
cout << &text << endl; //Display the Address of the Text
cout << *text << endl; //Display the Value of the Pointer to Text (The First Character of the Text Array)

return 0;
}

Share this post


Link to post
Share on other sites
Hey guys,

Here is the code:
if (event.EventType == EET_GUI_EVENT&&
event.GUIEvent.EventType==EGET_EDITBOX_ENTER)
{
consoleedit->getText();
ofstream fout("output.txt");
fout << consoleedit;

}

All it does, it takes the text that the user wrote into the editbox.the pointer consoleedit stores the adress to that text.
thing is that if i try to use what Radagar told me it still prints adress.

Thanks a lot.

Share this post


Link to post
Share on other sites
#include <iostream>
using namespace std;int main()
{
char* text = "Here is My Text\n";
cout << text << endl; //Display the Text
cout << &text << endl; //Display the Address of the Text
cout << *text << endl; //Display the Value of the Pointer to Text (The First Character of the Text Array)
return 0;
}

bugs:
1. you have assigned a string to a pointer that is not pointing to any allocated memory.
2. &text is not the address of the string its the address of the pointer
3. *text is the same as text[0] so its not the value just the first character

Share this post


Link to post
Share on other sites
What is the type of consoleedit? What does getText() return, if anything? Streams don't recognize pointers to anything other than char*, and I am very sure that consoleedit is not a char*. You either need to somehow get a char*, or a std::string object or reference. Or if consoleedit is a pointer to a class that you created, then you can overload the << operator yourself.

[Edit - Streams don't recognize pointers as anything else but pointers, i.e. integers. But they do recognize them.]

[edited by - Agony on February 26, 2004 3:21:44 PM]

Share this post


Link to post
Share on other sites
I assume that you need to assign consoleedit->getText() to something and send *that* to fout. A method call can''t magically change the type of consoleedit (whatever it is now) to char *.

Share this post


Link to post
Share on other sites
quote:
Original post by commander keen
bugs:
1. you have assigned a string to a pointer that is not pointing to any allocated memory.

This is totally legal. You''re smoking something.
quote:

2. &text is not the address of the string its the address of the pointer
As he states, clearly...
quote:

3. *text is the same as text[0] so its not the value just the first character
pray tell, what do YOU think text[0] is ? (hint: first character.)

I''m only being mean because you sounded so smart, except that you were wrong.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!