1

So I'm getting acquainted to C++ and have this problem when writing to a text file it also writes the memory location? here is my source code.

  std::ofstream userprefs;
srand(time(0));
int studentID = rand() % (99999 - 4 * 55) + 5 / 2;

std::string studentIDString = std::to_string(studentID);
userprefs.open("studentInformation.txt", std::ofstream::out | std::ofstream::app);

std::cout << "The Student ID is sID-"+ studentIDString +" (Copy everything including the sID.\nyou can also find this in the studentInformation.txt file.)"<< std::endl;

userprefs << std::cout << "the sID is sID-"+ studentIDString << std::endl;
userprefs.close();

When I open the text file it generates I get what I assume is memory?

0x804b164 the sID is sID-33921

How would I get this to either be hidden or just not generate this? And if it isn't memory can someone tell me what this actually is? I would also like to point out I'm using G++ with the build flag set to C++11.

1 Answers1

6

You are not supposed to do userprefs << std::cout. It is writing the address of std::cout in the file.

nishantsingh
  • 4,537
  • 5
  • 25
  • 51
  • Oh okay makes a lot more sense now. Thank you very much for your help just curious is there a time when ill ever need to use std::cout when writing to a file? Will mark as answer ASAP. – Twin Potato Sep 21 '16 at 04:11
  • You can read this: http://stackoverflow.com/questions/10150468/how-to-redirect-cin-and-cout-to-files – nishantsingh Sep 21 '16 at 04:12