4

Is there a way I can write text to a file from a certain point in the file?

For example, I open a file of 10 lines of text but I want to write a line of text to the 5th line.

I guess one way is to get the lines of text in the file back as an array using the readalllines method, and then add a line at a certain index in the array.

But there is a distinction in that some collections can only add members to the end and some at any destination. To double check, an array would always allow me to add a value at any index, right? (I'm sure one of my books said other wise).

Also, is there a better way of going about this?

Thanks

GurdeepS
  • 65,107
  • 109
  • 251
  • 387
  • Duplicate: http://stackoverflow.com/questions/98484/how-to-insert-characters-to-a-file-using-c – S.Lott Jan 04 '09 at 03:05
  • Similar to: http://stackoverflow.com/questions/289965/inserting-text-into-an-existing-file-via-java; http://stackoverflow.com/questions/125703/how-do-i-modify-a-text-file-in-python – S.Lott Jan 04 '09 at 03:06

3 Answers3

4

Oh, sigh. Look up the "master file update" algorithm.

here's pseudocode:

open master file for reading.
count := 0
while not EOF do
    read line from master file into buffer
    write line to output file    
    count := count + 1
    if count = 5 then
       write added line to output file
    fi
od
rename output file to replace input file
Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
2

If you're reading/writing small files (say, under 20 megabytes--yes I consider 20M "small") and not writing them that often (as in, not several times a second) then just read/write the whole thing.

Serial files like text documents aren't designed for random access. That's what databases are for.

cletus
  • 616,129
  • 168
  • 910
  • 942
1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class Class1
{                     
    static void Main()
    {
        var beatles = new LinkedList<string>();
        beatles.AddFirst("John");                        
        LinkedListNode<string> nextBeatles = beatles.AddAfter(beatles.First, "Paul");
        nextBeatles = beatles.AddAfter(nextBeatles, "George");
        nextBeatles = beatles.AddAfter(nextBeatles, "Ringo");

        // change the 1 to your 5th line
        LinkedListNode<string> paulsNode = beatles.NodeAt(1); 
        LinkedListNode<string> recentHindrance = beatles.AddBefore(paulsNode, "Yoko");
        recentHindrance = beatles.AddBefore(recentHindrance, "Aunt Mimi");
        beatles.AddBefore(recentHindrance, "Father Jim");

        Console.WriteLine("{0}", string.Join("\n", beatles.ToArray()));
        Console.ReadLine();                       
    }
}

public static class Helper
{
    public static LinkedListNode<T> NodeAt<T>(this LinkedList<T> l, int index)
    {
        LinkedListNode<T> x = l.First;

        while ((index--) > 0) x = x.Next;

        return x;
    }
}
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
Michael Buen
  • 38,643
  • 9
  • 94
  • 118