code_junkie

Writing text to the middle of a file

Nov 13th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. open master file for reading.
  2. count := 0
  3. while not EOF do
  4. read line from master file into buffer
  5. write line to output file
  6. count := count + 1
  7. if count = 5 then
  8. write added line to output file
  9. fi
  10. od
  11. rename output file to replace input file
  12.  
  13. public class Class1
  14. {
  15. static void Main()
  16. {
  17. var beatles = new LinkedList<string>();
  18.  
  19. beatles.AddFirst("John");
  20. LinkedListNode<string> nextBeatles = beatles.AddAfter(beatles.First, "Paul");
  21. nextBeatles = beatles.AddAfter(nextBeatles, "George");
  22. nextBeatles = beatles.AddAfter(nextBeatles, "Ringo");
  23.  
  24. // change the 1 to your 5th line
  25. LinkedListNode<string> paulsNode = beatles.NodeAt(1);
  26. LinkedListNode<string> recentHindrance = beatles.AddBefore(paulsNode, "Yoko");
  27. recentHindrance = beatles.AddBefore(recentHindrance, "Aunt Mimi");
  28. beatles.AddBefore(recentHindrance, "Father Jim");
  29.  
  30.  
  31. Console.WriteLine("{0}", string.Join("n", beatles.ToArray()));
  32.  
  33. Console.ReadLine();
  34. }
  35. }
  36.  
  37. public static class Helper
  38. {
  39. public static LinkedListNode<T> NodeAt<T>(this LinkedList<T> l, int index)
  40. {
  41. LinkedListNode<T> x = l.First;
  42.  
  43. while ((index--) > 0) x = x.Next;
  44.  
  45. return x;
  46. }
  47. }
Add Comment
Please, Sign In to add comment