Writing Data In Tabular format in Text File.

L

lucky

Hi guys,
i want to write some data in tabular format in text file. i've data in
strings and i want to write like this in file

col1 col2 col3

d1 d1 d1
d2 d2 d2
d3 d3 d3

the problem is, i dont know how can i maintain spaces between the
columns as i dont know which column will have what length of text.

in c++, i think u can do by adding padding to make sure the alignment
remains correctly but i dont know how can i do that in c#.

i would appriciate your help,

thanks
Lucky
 
J

Jeroen

Hi lucky,

Unfortunately, I think you should:

A. Try to find out what the maximum length is of a single cell in all
columns, and add enough whitespace to the other cells while writing to
the text file.

or

B. Start with a single whitespace, and each time you encounter a cell
that would require a previous cell in the column to have more
whitespace, to go back and review all cells in the column (and give
them more whitespaces).

Of course, option B is far more expensive, so you should really try to
know beforehand. I guess this is just a limitation of the constraints
you pose (use txt file and use whitespace).

Btw it feels like txt is perhaps not your best option here? Can't you
save in html and use tables or use a comma-delimited file?

Anyway, hope this helps.

Regards,
Jeroen


lucky schreef:
 
L

Lucky

hey pal,
thanks for replay. i'm thinking about the first option from the
begining but i wanted to take a chance to ask you people if someone
knows some other way of doing.

the reason why i can't use html or csv file is, i'm write data into the
log file and this file has different kind of data. The data that i need
to write is just a small part of it. that is why i didnt want to write
a long code to find the lenght of data and then format ir. but it
seems. i dont have another way..

thanks anyways,
Lucky
 
C

C-Services Holland b.v.

Lucky said:
hey pal,
thanks for replay. i'm thinking about the first option from the
begining but i wanted to take a chance to ask you people if someone
knows some other way of doing.

the reason why i can't use html or csv file is, i'm write data into the
log file and this file has different kind of data. The data that i need
to write is just a small part of it. that is why i didnt want to write
a long code to find the lenght of data and then format ir. but it
seems. i dont have another way..

thanks anyways,
Lucky

Erm.. put tabs between the columns? (vbTab)
 
O

Otis Mukinfus

Hi guys,
i want to write some data in tabular format in text file. i've data in
strings and i want to write like this in file

col1 col2 col3

d1 d1 d1
d2 d2 d2
d3 d3 d3

the problem is, i dont know how can i maintain spaces between the
columns as i dont know which column will have what length of text.

in c++, i think u can do by adding padding to make sure the alignment
remains correctly but i dont know how can i do that in c#.

i would appriciate your help,

thanks
Lucky

Lucky,

Go take a look at String.Format in the .NET 2.0 (or 1.1) documentation. In C#
string.Format is somewhat like sprintf in C.

Example:

string s = string.format("{0,10:0.00}", 10); // yields "10.00 "
string s = string.format("{0,-10:0.00}", 10); // yields " 10.00"
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
L

Lucky

Hi Otis,
thanks for pointing out the capability of String Class. I can use that
instead of using the Padding funtion of the String Class.
by the way the Question is, how to determine how many spaces i need to
provide between two columns to keep the alignment.

Jeroen and i were discussing that. what u suggest that i can use after
i determine the number of spaces in betweeen two columns.

have a nice day,
Lucky
 
O

Otis Mukinfus

Hi Otis,
thanks for pointing out the capability of String Class. I can use that
instead of using the Padding funtion of the String Class.
by the way the Question is, how to determine how many spaces i need to
provide between two columns to keep the alignment.

Jeroen and i were discussing that. what u suggest that i can use after
i determine the number of spaces in betweeen two columns.

have a nice day,
Lucky

Lucky,

I suppose you could first read the data and calculate the widths of the data
elements and then create the format string, but I would set the widths and
column alignments beforehand. To do that you would need to know the maximum
expected width of each data element you are going to display. Doing so would
give you a fixed width format for all rows.

One thought I have (without knowing how you would be using the tabular output)
is that adjusting the width to the data during the run would make the data in
the output difficult to parse if you were to need to read it from a file later.

If you are doing this just for display and want to adjust each column for the
widest existing data in each column would, it seems to me, require that you read
the file one time to calculate the needed column widths, construct the format
string, and then read the file once more to do the output. I know that sounds
like it might slow things down, but you might be surprised how fast the
measuring operation would be.

All of the above assume you need the final output to be human readable. If the
file must also be machine readable you will have to use a fixed width known to
the second machine read routine.

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
L

Lucky

hey Otis,

yeah, i'm doing something like that. now i'm keeping the longest length
of the each field in the table, into one variable, while i'm using the
data and when i go for the writing the data into the log file(which is
just for display purpose or u can say it's for the developers to see
how things are going around), i'm formating the string the way u
suggested( i've modified the code after your suggestion of
String.Format() function) and wrting the data prefectly into log file.

thanks for your help guys,

Lucky
 
O

Otis Mukinfus

hey Otis,

yeah, i'm doing something like that. now i'm keeping the longest length
of the each field in the table, into one variable, while i'm using the
data and when i go for the writing the data into the log file(which is
just for display purpose or u can say it's for the developers to see
how things are going around), i'm formating the string the way u
suggested( i've modified the code after your suggestion of
String.Format() function) and wrting the data prefectly into log file.

thanks for your help guys,

Lucky

Thanks Lucky. It's always fun to work out the brain muskles ;O)
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 

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

Top