Tomas Michalek Tomas Michalek - 4 months ago 20
C# Question

C# store file lines into array, then save chosen lines

I am trying to make code, that will basically open desired file, store all lines into array, and then, if line does fulfill condition, line will be save into new text file. When running program, I get error message to line reader, that index is outside of array. Please help, I am new to C#

int NumLines = new StreamReader(OpenFile.FileName).ReadToEnd().Split(new char[] { '\n' }).Length;
StreamReader Data = new StreamReader(OpenFile.FileName);
string[] arr = new string[NumLines];
for (int i = 0; i <= NumLines; i++)
{
StreamWriter File = new StreamWriter("C://Users//Tomas//Desktop//BC//tmp//Data.txt");
arr[i] = Data.ReadLine();

Answer Source

Index should be less than array size:

for (int i = 0; i < NumLines; i++)
                  ^

But you don't need to put lines into array in order to write them to other file. E.g. if you want to write only some lines by condition:

File.WriteAllLines(newFileName, File.ReadLines(OpenFile.FileName).Where(condition));

Where condition is a delegate which checks whether line should be written. E.g. if you want to write only lines which start with "Foo":

.Where(line => line.StartsWith("Foo"))

NOTE: If you want to write all lines to new file without condition, then you simply can copy original file:

File.Copy(OpenFile.FileName, newFileName);

UPDATE: From your comments, it's clear that you need to find line which matches some condition and also write to output 6th and 8th lines after that line. You still can do that without dumping all source file into memory:

private static IEnumerable<string> GetVertexCoordinates(string fileName)
{
    var enumerator = File.ReadLines(fileName).GetEnumerator();

    while (enumerator.MoveNext())
    {
        if (enumerator.Current == "VERTEX")
        {
            yield return enumerator.Current;

            for (var i = 0; i < 6; i++) enumerator.MoveNext(); // skip 6 lines
            yield return enumerator.Current; 

            for (var i = 0; i < 2; i++) enumerator.MoveNext(); // skip 2 more lines
            yield return enumerator.Current;
        }
    }
}

And usage is still one-liner

File.WriteAllLines(newFileName, GetVertexCoordinates(OpenFile.FileName));