Click here to Skip to main content
15,896,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i need to write data to file in specified position with fixed length for each column with start position,end position and length of column. could you please alter my code or suggest.I have given sample length for each column

C#
Column	startposition  endposotion  Length
     1		1	        9	         9	
     2		10	       20	         10
     3		21          29           8
     4      30          31           1
     5		32	        42          10





Output i needed to write in text file as:

C#
00000	  2014-09-23 01:36:22 F	2014-09-23
000000	  2014-09-23 01:36:22 F	2014-09-23
000000000 2014-09-23 01:36:22 F	2014-09-23


below is code which i have created

string txt = string.Empty;
DataTable employeeTable = new DataTable("Employee");
employeeTable.Rows.Add("00000", "2014-09-23", "01:36:22", "F", "2014-09-23");
employeeTable.Rows.Add("000000", "2014-09-23", "01:36:22", "F", "2014-09-23");
employeeTable.Rows.Add("000000000", "2014-09-23", "01:36:22", "F", "2014-09-23");
DataSet ds = new DataSet("Organization");
ds.Tables.Add(employeeTable);
string filepath = Application.StartupPath + "\\" + "file.txt" ;
StreamWriter sw = null;
sw = new StreamWriter(filepath, false);
foreach (DataRow row in ds.Tables[0].Rows)
{
foreach (var item in row.ItemArray)
{
//Add the Data rows.
txt += item.ToString() + "\t";
}
txt += "\r\n";
}
sw.Write(txt);
sw.Close();


OutPut which iam getting from this code is, but this is wrong.
C#
00000	2014-09-23	01:36:22	F	2014-09-23	
000000	2014-09-23	01:36:22	F	2014-09-23	
000000000	2014-09-23	01:36:22	F	2014-09-23


kindly rectify my problem.

What I have tried:

string txt = string.Empty;
DataTable employeeTable = new DataTable("Employee");
employeeTable.Rows.Add("00000", "2014-09-23", "01:36:22", "Falseis1", "2014-09-23", "01:36:22");
employeeTable.Rows.Add("000000", "2014-09-23", "01:36:22", "F", "2014-09-23", "01:36:22");
employeeTable.Rows.Add("000000000", "2014-09-23", "01:36:22", "F", "2014-09-23", "01:36:22");
DataSet ds = new DataSet("Organization");
ds.Tables.Add(employeeTable);
string filepath = Application.StartupPath + "\\" + "file.txt" ;
StreamWriter sw = null;
sw = new StreamWriter(filepath, false);
foreach (DataRow row in ds.Tables[0].Rows)
{
foreach (var item in row.ItemArray)
{
//Add the Data rows.
txt += item.ToString() + "\t";
}
txt += "\r\n";
}
sw.Write(txt);
sw.Close();
Posted
Updated 28-Oct-19 5:04am
v10
Comments
Sinisa Hajnal 1-Dec-17 2:53am    
Since your columns are of different lengths, you cannot use tab as separator (fixed length separator). Instead, determine the longest text in each column and set that + 1 space (or whatever number of spaces fits your case). You need to pad all other strings to that max length + spaces and then concatenate them.
Vinodh Muthusamy 1-Dec-17 3:47am    
couldyou please modify my code
Richard MacCutchan 1-Dec-17 4:24am    
It should be obvious that you need to create each text item in a fixed width field. string.Format will help you do that.

After you have converted an object to a string, you can use the PadLeft and PadRight methods to justify them into a fixed length string:
C#
object o1 = "0000";
object o2 = "00000";
Console.WriteLine("\"" + o1.ToString().PadLeft(8) + "\"");
Console.WriteLine("\"" + o2.ToString().PadRight(8) + "\"");
Will give you:
"    0000"
"00000   "
You can use this on each field to give you the fixed widths you need.
 
Share this answer
 
Comments
Vinodh Muthusamy 1-Dec-17 4:33am    
But i need to start with specified start position of each columns
OriginalGriff 1-Dec-17 4:54am    
And if you start at the beginning of the record, and pad each field to the right length as you write them, that is what you will get...
Don't use string concatenation in a loop! You've already got a StreamWriter which you can use to write each part of the string to.

And the simplest option for fixed-width output is to use the alignment option of the string formatting feature:
Composite Formatting | Microsoft Docs[^]
C#
using (StreamWriter sw = new StreamWriter(filepath, false))
{
    foreach (DataRow in ds.Tables[0].Rows)
    {
        writer.WriteLine("{0,-9}{1,-10}{2,-8}{3}{4,-10}", row.ItemArray);
    }
}
Output:
00000    2014-09-2301:36:22F2014-09-23
000000   2014-09-2301:36:22F2014-09-23
0000000002014-09-2301:36:22F2014-09-23
 
Share this answer
 
Comments
Vinodh Muthusamy 5-Dec-17 2:50am    
thanks Richard, it sounds good.its working
Member 13968331 31-Aug-18 4:03am    
what am I going to do in order for the output to be aligned specially the sign(*) since when I outputted it it is not align and how to make that program in which i will update a certain line
You can use the AGPC.FixedLayout Nuget to do it in a simply way
 
Share this answer
 
Comments
CHill60 29-Oct-19 9:36am    
Beware - this could be perceived as site driving

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900