Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Format and write a .net dataset to fixed length file format.

Posted on 2006-11-05
9
Medium Priority
?
461 Views
Last Modified: 2012-05-05
I have a result set in a C# console app. I want to write each row to a fixed length file. ie: each resultset element will take x amount of spaces that I define. How do you do this in C#?
0
Comment
Question by:claghorn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 15

Accepted Solution

by:
ozymandias earned 60 total points
ID: 17877391
first create an array of int with each int representing the length of each field in your fixed width field.

<pseudo-code>

int[] fieldLength = new int[]{5,10,25};
foreach (row in dataset){
    foreach(field in row){
        string s = field.ToString();
        s = s.PadRight(fieldLength[fieldnumber], ' '); // pads the string out to the specified length with spaces
        add s to your fixed width record
    }
    add fixed width record to fixed width file
}

</pseudo-code>
0
 

Author Comment

by:claghorn
ID: 17878176
close, but it does not terminate each line in the resulting file. ie: the line continues infinitem. btw putting "\n" puts a space between each line and does not terminate each line either. Can you suggest the rest for a full soultion?
0
 
LVL 11

Expert Comment

by:Expert1701
ID: 17879121
The string, "\r\n", should terminate the line.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:claghorn
ID: 17881052
Thanks, but no luck. It does not terminate the line. ie: if you cursor (right arrow) over the line it continues beyond.
Also, the lines are spaced now.
I always did get the return to next line when not specifying the "\r\n", but again, the line continues beyond the end.
The file does not behave like a main frame generated file that, when you cursor through it, it returns you to the next line.

0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17881152
Lines cannot possibly continue beyond the end.
The end is, by definition, the point beyond which the line does not continue.
The most likely explanation is that the lines does not end where you expect them to, i.e. you have padded the last field of the record out with spaces.
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17902712
Did you have any luck with this ?
0
 

Author Comment

by:claghorn
ID: 17911061
If I try to put a \r or \n, I get a space separating each line. This is unwanted. I want to be able to scroll through each line and where the data ends, find myself at the beginning of the next line.  
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17912535
OK. Is it possible that you are creating the record adding a \n or a \r (or both) to the end of it and then using the WriteLine() method of some stream/file/text writer that is appending another \r\n to the end of the line ?

It would be useful if you showed your code but to be honest for a 20 point question you can't expect miracles.
The best you can hope for for 20 points is to be pointed in the right direction and if you are lucky some pseudo-code.
It's a bit much to then go and give a B grade because you can't get the linefeeds to work.
0
 
LVL 3

Expert Comment

by:baldrick
ID: 17938047
Use System.Environment.NewLine, not "\r\n"
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question