473,387 Members | 1,834 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,387 software developers and data experts.

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

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
6 13399
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Coco | last post by:
can i transform fixed length text to xml using XSL do i have to first load the text to xml and do the transformation? is there any other way to do it?
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...
2
by: Darin Browne | last post by:
I have to create a fixed length text file and was hoping someone could point me to an example or resources that could help explain how it is done in C#. Thanks.
0
by: suryar | last post by:
Hi please help me some one in my company i want to write a script in vb to write a text file but the source file is an excel file i want write a text or dat file with fixed lengths maping with ...
4
by: sanparab | last post by:
I am a new user for MS Access 2003, I want to transfer a data of Fixed lenght text file into Access Tabel. The data is contained of four Record Set those are as follows 1. The header record...
0
by: Mic | last post by:
Hi, I just started using Visual Web Developer Express 2005. I need to print a dataset that is populated from a fixed length text file. No problem with this part. So I am trying to use a...
6
by: ssharpjr | last post by:
Hi Guys, I'm new to Python (mostly) and I'm wanting to use it for a new project I'm faced with. I have a machine (PLC) that is dumping its test results into a fixed- length text file. I need...
2
by: Edwin.Madari | last post by:
#your thought is right. ======================================================= def sizes2fields(sizes): d = begin = 0 for i in sizes: if begin: end = begin + i else: end = i...
8
by: iheartvba | last post by:
Hi I am using Access 2007 and am trying to export a query to a fixed length text file. I tried using the following code to export the text file: DoCmd.TransferText acExportFixed, , "qryFFRDeFile",...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.