2016-09-21 63 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