我想要做的一個字符串:在文件輸入和存儲可以是任意長度的用戶希望
存儲記錄。這些記錄有兩件事。
time_t rt; //which stores the time the record was entered by the user
並伴隨此我想存儲一個字符串。但我不知道字符串的長度。
它將根據運行時間決定,並取決於用戶輸入的字符數。
什麼需要做的(據我):
我不知道。我知道動態內存分配,但不知道如何將此應用於這樣的問題。
我曾嘗試:
我試圖把一個charachter在來自用戶的時間,並將其存儲在一個文本文件(暫時)。
ofstream fileObject;
fileObject.open("temp.txt");
for(int j=0;;j++)
{
ch = _getche();
if(ch == 13) break; //user has pressed the return key
fileObject<<ch;
}
然後我發現了文件的大小使用以下代碼:
fileObject.seekp(0,ios::end);
long pos = fileObject.tellg(); //this is the size of the file
然後我聲明的文件的大小的動態數組。
char * entry;
entry = new char[pos]
在「out」模式下關閉文件並在「in」模式下再次打開文件。
fileObject.close();
ifstream fout;
fout.open("temp.txt"); //this is the name of the text file that i had given
然後性格聰明我複製的文本文件的內容到字符數組:
for(int i=0;i<pos;i++)
fout>>info[i];
info[i] = '\0';
fout.close();
但現在我不知道該怎麼做進一步。
我需要你什麼來幫助我:
幫我寫這個記錄作爲一類對象轉換成二進制「.DAT」文件。
我的規格:
的Windows XP SP 3
IDE:的Visual C++ 2010速成
C++?你有沒有考慮過使用std lib中的字符串? – 2013-02-25 09:21:26
先生,問題已解決 – IcyFlame 2013-02-25 09:27:19
是的。我被要求使用它。 – IcyFlame 2013-02-25 09:27:37