2016-09-21 62 views
1

因此,我熟悉C++,写入文本文件时遇到了这个问题,它也写入了内存位置?这里是我的源代码。C++ 11写入文件也写出内存位置?

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(); 

当我打开文本文件时,它产生我得到我所假设的是内存?

0x804b164 the sID is sID-33921 

我怎样才能得到这个要么隐藏或只是不生成这个?如果这不是记忆,有人可以告诉我这是什么吗?我还想指出,我正在使用带有设置为C++ 11的构建标志的G ++。

回答

6

你不应该这样做userprefs << std::cout。它在文件中写入std::cout的地址。

+0

哦,好吧,现在有更多的意义。非常感谢你的帮助,只是很好奇,在写入文件时,有没有什么时候需要使用std :: cout?将标记为答案尽快。 –

+0

你可以阅读:http://stackoverflow.com/questions/10150468/how-to-redirect-cin-and-cout-to-files – user3286661