我想做的事:
将记录存储在文件中。这些记录有两点。
time_t rt; //which stores the time the record was entered by the user
除此之外,我还想存储一个字符串。但我不知道字符串的长度。
这将取决于运行时间,并取决于用户输入的字符数。
需要做什么(根据我):
我没有线索。我知道动态内存分配,但不知道如何将其应用于此类问题。
我试过的:
我试图一次从用户那里获取一个字符并将其存储在一个文本文件中(临时)。
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”文件。
我的规格:
视窗 XP SP 3
IDE:Visual C++ 2010 Express