peter
peter

Reputation: 8682

How to write a word after 50 lines in text file

I am writing a string in to a text file by using File.AppendAllLines ,now i want to write a word 'Pause' after every 50 lines, how to do that in File.AppendAllLines code? this is the code i am doing

string test;
foreach (DataRow dr in dt.Rows)
{
    test=GetData(dr);
    File.AppendAllLines("tEST.txt", new string[] { test });
}

Upvotes: 0

Views: 205

Answers (3)

Harald Coppoolse
Harald Coppoolse

Reputation: 30474

As you are using the Linq tag, I assume you you'd like to convert your code into linq statements.

Although you don't mentions it, I think your dt is a DataTable and dt.Rows returns a DataRowCollection. This collection implements IEnumerable<Row>. Apparently your Row has a GetData which returns a string.

So the step to convert your dt.Rows into strings is easy:

var linesWithoutPause = dt.Rows    // returns DataRowCollection
    .Cast<Row>()                   // converts to IEnumerable<Row>
    .Select(row => row.GetData()); // from exvery Row extract the data

Now you want to write the word "Pause" after every 50th line. You didn't mention whether you want to append this word to every 50th line, or whether you want to append it as a separate line. The code will be very similar.

To make it Linq like, it is best to create an extension function for it. For more information about extension functions see Extension Functions Demystified

We'll make a generic extension function of IEnumerable, which will add an object of type after every count items. This way you will be able to use it for all kinds of collections: append 0 after every 10 integers; append a beep sound after every 20 sounds etc.

Extension functions must be in a static class. The name is not important:

static class EnumerableExtensions
{
    // add your extension functions here
}

The extension function is fairly easy. It must be static, and the first parameter must be of type IEnumerable and preceded by the keyword this. Inside the function the this parameter is uses as if if was without this:

public static AppendWordEveryCount<T>(this IEnumerable<T> source, T itemToAppend, int count)
{
    int counter = 0;                    // a counter to find every count element
    foreach (T item in source)
    {
        yield return T;                 // just return the item in your sequence
        counter = (counter + 1) % count;
        if (i == 0)                     // after count elements
        {
             yield return wordToAppend; // append the item
        }
    }
}

Now back to your question: you want to append "Pause" as a separate line after every 50th line:

const string wordToAppend = "Pause";
const int lineCount = 50;

var result = dt.Rows
    .Cast<Row>()
    .Select(row => row.GetData())
    .AppendWordEveryCount(wordtToAppend, lineCount);

Upvotes: 0

Cars Ten
Cars Ten

Reputation: 53

Switch foreach loop to for loop and check loop control value for modulo 50.

for (int i = 0, i < dt.Rows.Count (), i++ )
{
   test=GetData(dt.Rows[i]);`
   if(i % 50 == 0) 
   //Pause
   File.AppendAllLines("tEST.txt", new string[] { test });

}

Upvotes: 4

C&#225;t Bụi
C&#225;t Bụi

Reputation: 1

There are 2 solution in this case:

case1: If in runtime, you append line by line to file. You can using an variable type int. For each you append line to file, this variable increment. You check this variable equals 50, you append "Pause" line to your file.

Case 2: If you have exists file with about 1000 lines, you can use split with '\n'. And using for loop to do this case

Upvotes: 0

Related Questions