Hi Roberto,
( Name )( Gender )( Age ) -> Field
( 1 - 12 )( 13 - 24 )( 25 - 36 ) -> Columns delimitation
Genaro Female 24
Raimunda Male 32
Severino Female 18
Ok, although manipulators can be used for input, there is no need to use
them like this. Alternatives are get, getline and read, which read
characters into a specified buffer, but this can cause overflow issues
'cause you have to handle the terminators (get, getline). Another way, that
won't cause issues with overflow, is to read the data into a string and
check the size or length of the input as you get it. If it exceeds the max,
throw an exception or prompt for the data again or whatever. Here are
some examples:
const unsigned int kuiMaxWidth = 12;
if (sName.length() > kuiMaxWidth)
{
//throw objTooLong;
}
if (sGender.length() > kuiMaxWidth)
{
//throw objTooLong;
}
if (sAge.length() > kuiMaxWidth)
{
//throw objTooLong;
}
sFilename = "test_file_1";
ofstream outfile(sFilename.c_str());
outfile << setw(kuiMaxWidth) << sName << sGender << sAge << endl;
outfile << endl;
Or, maybe something like
const unsigned int kuiMaxWidth = 12;
const char mkcPadChar = ' ';
if (sName.length() <= kuiMaxWidth)
{
sName.append((kuiMaxWidth - sName.length()), mkcPadChar);
}
else
{
//throw objTooLong;
}
if (sGender.length() <= kuiMaxWidth)
{
sGender.append((kuiMaxWidth - sGender.length()), mkcPadChar);
}
else
{
//throw objTooLong;
}
if (sAge.length() <= kuiMaxWidth)
{
sAge.append((kuiMaxWidth - sAge.length()), mkcPadChar);
}
else
{
//throw objTooLong;
}
sFilename = "test_file_2";
ofstream outfile(sFilename.c_str());
outfile << sName << sGender << sAge << endl;
outfile << endl;
Or, you can write to a string stream (eg. ostringstream) prior to writing to
the output file stream. There are many ways to do this.
That's ok, no need to apologise.
Regards,
M