By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,018 Members | 1,204 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,018 IT Pros & Developers. It's quick & easy.

How can we generate a fixed length text file in C#?

P: n/a
Hi,
I have to generate a fixed length text file.I have a file formats like
fields details as well as its length and position.what actually i have to do
that i will get the data from the databas and then i need to create a fixed
length flat file as per format.so please give me a sample for this.
Jul 19 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Jul 19, 12:59*pm, Tarun <Ta...@discussions.microsoft.comwrote:
Hi,
I have to generate a fixed length text file.I have a file formats like
fields details as well as its length and *position.what actually i haveto do
that i will get the data from the databas and then i need to create a fixed
length flat file as per format.so please give me a sample for this.
Your question is extremely broad, so the answer is going to be the
same, too: use StreamWriter class and WriteLine method, and for the
latter specifically, use format strings to generate your output. For
instance, if you have one 80-char left-aligned string field followed
by one 20-char right-aligned integer field, you'd do something like
this:

string field1;
int field2;
...
using (var writer = new StreamWriter("output.txt"))
{
writer.WriteLine("{0,-80}{1,20}", field1, field2);
}

Note that using alignment (the number after comma) in format
specifiers will pad the value with required number of spaces on the
left (for positive values) or on the right (for negative values) as
needed, but it will not truncate the value if it's longer than the
alignment. So if there's a possibility that string is longer than 80
chars, or that textual representation of integer is longer than 20
chars, you'll need to handle those cases specially as per your output
format specification.
Jul 19 '08 #2

P: n/a
This sounds like a homework question.

Mike.

"Pavel Minaev" <in****@gmail.comwrote in message
news:c9**********************************@b1g2000h sg.googlegroups.com...
On Jul 19, 12:59 pm, Tarun <Ta...@discussions.microsoft.comwrote:
Hi,
I have to generate a fixed length text file.I have a file formats like
fields details as well as its length and position.what actually i have to
do
that i will get the data from the databas and then i need to create a
fixed
length flat file as per format.so please give me a sample for this.
Your question is extremely broad, so the answer is going to be the
same, too: use StreamWriter class and WriteLine method, and for the
latter specifically, use format strings to generate your output. For
instance, if you have one 80-char left-aligned string field followed
by one 20-char right-aligned integer field, you'd do something like
this:

string field1;
int field2;
....
using (var writer = new StreamWriter("output.txt"))
{
writer.WriteLine("{0,-80}{1,20}", field1, field2);
}

Note that using alignment (the number after comma) in format
specifiers will pad the value with required number of spaces on the
left (for positive values) or on the right (for negative values) as
needed, but it will not truncate the value if it's longer than the
alignment. So if there's a possibility that string is longer than 80
chars, or that textual representation of integer is longer than 20
chars, you'll need to handle those cases specially as per your output
format specification.


Jul 19 '08 #3

P: n/a
School is supposed to teach you lessons for use in the commercial world of
employment. Any coder who doesn't use Google and/or newsgroups to research
the answers to questions in my opinion wastes time, so I would mark this
homework as "Passed" :-)

Jul 19 '08 #4

P: n/a
Peter Morris wrote:
School is supposed to teach you lessons for use in the commercial world
of employment. Any coder who doesn't use Google and/or newsgroups to
research the answers to questions in my opinion wastes time, so I would
mark this homework as "Passed" :-)
But by giving the student a degree the school certify that the
student is able to find a solution also in cases where the internet
search turn up empty.

Arne
Jul 19 '08 #5

P: n/a
But by giving the student a degree the school certify that the
student is able to find a solution also in cases where the internet
search turn up empty.
Which is exactly what he is doing :-)
Jul 26 '08 #6

P: n/a
Peter Morris wrote:
>But by giving the student a degree the school certify that the
student is able to find a solution also in cases where the internet
search turn up empty.

Which is exactly what he is doing :-)
Not if you consider usenet to be part of internet.

Arne
Jul 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.