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.