Reputation: 5209
What I want to do:
Store records in a file. These records have two things.
time_t rt; //which stores the time the record was entered by the user
and along with this I want to store one string. But I don't know the length of the string.
It will be decided on run time and will depend on how many characters the user enters.
What needs to be done(According to me):
I have no clue. I know about dynamic memory allocation but did not know how to apply this to such a problem.
What I have tried:
I have tried to take one charachter at a time from the user and store it in a text file(Temporarily).
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;
}
Then I found out the size of the file using the following code:
fileObject.seekp(0,ios::end);
long pos = fileObject.tellg(); //this is the size of the file
Then I declared a dynamic array of the size of the file.
char * entry;
entry = new char[pos]
Closed the file in the "out" mode and opened it again in the "in" mode.
fileObject.close();
ifstream fout;
fout.open("temp.txt"); //this is the name of the text file that i had given
Then character wise I copied the content of the text file into the character array:
for(int i=0;i<pos;i++)
fout>>info[i];
info[i] = '\0';
fout.close();
But now i dont know what to do further.
What I need you to help me with:
Help me to write this record as a class object into a binary ".dat" file.
My specs:
Windows XP SP 3
IDE: Visual C++ 2010 Express
Upvotes: 1
Views: 102
Reputation: 141810
I want to store one string. But I don't know the length of the string.
Then you need to use std::string
and not a preallocated array of char
s.
struct user_record
{
time_t rt; //which stores the time the record was entered by the user
std::string one_string;
};
Help me to write this record as a class object into a binary ".dat" file.
There are a number of serialisation options available to you. Perhaps the simplest is to write this as plain text using the standard stream operations:
std::ostream& operator <<(std::ostream& os, user_record const& ur)
{
return os << ur.rt << ' ' << ur.one_string;
}
std::istream& operator >>(std::istream& is, user_record& ur)
{
return is >> ur.rt >> ur.one_string;
}
For anything more involved than a single-line string, then perhaps you should investigate Boost's serialisation library.
Upvotes: 1
Reputation: 153919
What are the restrictions on the string? And how do you recognize that the user has entered all of the data he wants in the string?
If the string has to be a single line, and we can assume
"reasonable" length (i.e. it will easily fit into memory), then
you can use std::getline
to get the string into an
std::string
(for input), and then define the output format,
say "%Y-%m-%d %H:%M:%S: user string\n"
for
the file. If the user string can be several lines, you'll have
to define a protocol to input them (so you can know when
a single record is finished), and a more complex format for the
file: one suggestion would be to separate records by an empty
line (which means that the input cannot contain an empty line),
or to use a record header along the lines of: "%Y-%m-%d
%H:%M:%S line_count\n"
. (Subversion uses
a variant of this for it's commit
messages. With a bit more
information, however, but the timestamp and the number of lines
are there.)
Upvotes: 1
Reputation: 145279
use std::string
and std::getline
, both from the <string>
header
Upvotes: 1