Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reformatting a Ragged Right File

Status
Not open for further replies.

bruce282

Programmer
Jan 26, 2007
8
US
Input is a ragged right text file, not all lines are the same length. The output has to be another text file with each record being 574 bytes long.

I'm a Cobol guy that's done some C and VB coding. In VB I would just open the file, read a record into deers_in (see below), then just write the record to my output file.

Dim deers_in As String * 574

I'm having a hard time translating the above statement to something that would work in C#. I'm using the Express C# 2008 version.

Thanks,

Bruce
 
if the files not too big you can read the entire file into memory. if these are large files you may want to take a look at FileHelpers or FilePathDirectory to manipulate large files. in either case something this like should work
Code:
var path = "path to file";
var clean = new System.Text.StringBuilder();
var ragged = System.IO.File.ReadAllLines(path);
foreach(var line in ragged)
{
    clean.Append(line);

    var missing = 574 - line.Length;
    for(var i = -1; i < missing;i++)
    {
        clean.Append(string.Empty);
    }
    clean.AppendLine();
}
System.IO.File.WriteAllText(path, clean.ToString());

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top