a brief help on generating fixed column text files

R

Roberto Dias

Hi all,

I've got some column delimited text files used as input file to a
power engineering application. I'm trying to create a "more-friendly"
interface using C++. The C++ program should be able to put the entered
data in correct columns. I know this should be a easy task, but I
simply cannot implement this. I have trying to use <iomanip> (setw,
setiosflags,...), but it doesn't function. Here is a piece of the
program:
 
W

who

Hi Roberto,

Please be more specific about the problems you're having. The way you use
the manipulators limit the width, set and clear flags in the input stream
(cin), what is it that doesn't function here? How are you reading the file?
Maybe supply more code that actually makes more sense.
I've got some column delimited text files used as input file to a
power engineering application. I'm trying to create a "more-friendly"
interface using C++. The C++ program should be able to put the entered
data in correct columns. I know this should be a easy task, but I

Are you having problems reading the file or storing the columns?
simply cannot implement this. I have trying to use <iomanip> (setw,
setiosflags,...), but it doesn't function. Here is a piece of the
program:

What do you mean by "implement". What are you expecting the manipulators to
do? I modified your code slightly and it works as expected, so again what is
the issue?

Regards,

M
 
R

Roberto Dias

who said:
Hi Roberto,

Please be more specific about the problems you're having. The way you use
the manipulators limit the width, set and clear flags in the input stream
(cin), what is it that doesn't function here? How are you reading the file?

I am interested in create a text file, whose each line has fixed width
fields, for example:

( Name )( Gender )( Age ) -> Field
( 1 - 12 )( 13 - 24 )( 25 - 36 ) -> Columns delimitation
Genaro Female 24
Raimunda Male 32
Severino Female 18
Maybe supply more code that actually makes more sense.


Are you having problems reading the file or storing the columns?

creating files
What do you mean by "implement". What are you expecting the manipulators to
do? I modified your code slightly and it works as expected, so again what is
the issue?

I expected the manipulators to limit the number of characters entered
by the "user", but a different thing happend: when the user tried to
enter more then the said by "setw(12)", for example, the program flow
was interrupted and wrong numbers were placed in rest of field not yet
filled.
Regards,

M
Sorry the poor English

Roberto Dias
Recife/PE-Brazil
 
J

John Harrison

I expected the manipulators to limit the number of characters entered
by the "user", but a different thing happend: when the user tried to
enter more then the said by "setw(12)", for example, the program flow
was interrupted and wrong numbers were placed in rest of field not yet
filled.

That is wrong, manipulators don't work like that.

What is it that you want to happen when the user enters to much data?
C++ cannot stop the user entering as much data as they want to, it is your
job to find out that they have entered too much data and do something
about it.

john
 
W

who

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.
Sorry the poor English

That's ok, no need to apologise.

Regards,

M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top