Chaitanya
Chaitanya

Reputation: 379

How to pad the required content to a text file

Hi all i am doing an application where i write my data to the text file. What ever data that user enters on the form and click on save i will save that data to the text file that was chosen by the user . Assume my content is as follows

1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234

I would like to pad the next 8 lines with the following 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999

Lie that if i have 5 lines of text in the file i would like to pad the next 5 lines with the same as mentioned can any one tell how to do this

Each and every line length is '94'

Any number of lines can be there 

Upvotes: 0

Views: 447

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460028

var text = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234" + Environment.NewLine + "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234";
const String padWith = "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999";
const int lineNum = 10;
var lines = text.Split(new[] { Environment.NewLine }, StringSplitOptions.None).ToList();
while(lines.Count < lineNum) {
    lines.Add(padWith);
}
File.WriteAllLines(path, lines);

Upvotes: 2

Developer
Developer

Reputation: 8636

Here goes the code.

1) Find out the no of lines in your file

2) make count%10, if count%10==0 do not pad else 10-result=required length , Pad with required length.

Sample code assume you have 8 lines

    int cnt = 8; 
    int result = cnt % 10;  // Will get 8
    int iresult1 = 10 - result;

Hope it helps

Upvotes: 0

Related Questions