473,378 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

a brief help on generating fixed column text files

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:

Jul 22 '05 #1
4 1726
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
Jul 22 '05 #2
"who" <so*****@somewhere.com> wrote in message news:<rD******************@news-server.bigpond.net.au>...
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.
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?


creating files
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?


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
Jul 22 '05 #3
On 10 Jul 2004 05:28:08 -0700, Roberto Dias <di*****@br.inter.net> wrote:


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
Jul 22 '05 #4
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

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

179
by: SoloCDM | last post by:
How do I keep my entire web page at a fixed width? ********************************************************************* Signed, SoloCDM
5
by: Johnny Meredith | last post by:
I have seven huge fixed width text file that I need to import to Access. They contain headers, subtotals, etc. that are not needed. There is also some corrupt data that we know about and can...
0
by: Masa Ito | last post by:
I have large fixed width text files that I need to parse. I regex'd a file that defines the columns - so I now have a large arraylist of column headers and column widths. I can read a line of...
1
by: Ryan Ginstrom | last post by:
I have been maintaining a body of documentation in plain HTML files. I would now like to automate the generation of the HTML files. Maintaining the HTML files now is tedious and error prone,...
31
by: Sarita | last post by:
Hello, this might sound stupid, but I got a really nice homepage template which unfortunately is a 3-Column Fixed Width CSS format. Now I don't have any content for the right column and would...
7
by: Nathan Sokalski | last post by:
I am an ASP.NET developer, and Visual Studio 2005 seems to have stopped declaring the controls that I add in the *.designer.vb files, therefore forcing me to manually add them before I can use them...
1
by: Nathan Sokalski | last post by:
Visual Studio 2005 recently stopped generating the *.designer.vb files for my *.aspx and *.ascx files. I am using Service Pack 1, and do not believe I did anything differently than normal prior to...
1
by: Nathan Sokalski | last post by:
Visual Studio 2005 unexpectedly stopped generating the *.designer.vb files for *.aspx and *.ascx files. After a few days of frustration trying to fix this, I noticed that it had the following...
4
by: Jeff | last post by:
Hey I'm wondering how the Fixed-Width Text Format is What I know is that the top line in this text format will contain column names. and each row beneath the top line represent for example a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.