Fliping lots of text lines... ?


I'm using Windows 7 64bit with Notepad++ 5.9.3, Visual Studio 2010 Ultimate, C# .NET 2.0.


So i face a really annoying problem today,
I had an array with 400 lines written in C#.
The problem is that, the list was reversed from the order i wanted.


The array i have:

The array i need:

Like i said it's a 400 lines array...


I really needed this reversed, i tried Notepad++ with several plugins like TextFX without any success, i actually found out that it's possible to do it by recording a Macro in the Notepad++, but it's like not my thing.

So i used my best friend! C# :)

I'll make a quick explanation of what i'm doing and then just paste the code.


So i made a Console App that gets a file path from the application arguments.
It reads all the lines and pushs them into a Stack then writes it to a new file using Pop :)

Heres the code: (Just compile and it works)

class Program
 static void Main(string[] args)
  if (args == null)
   Console.WriteLine("args is null"); // Check for null array
   if (args.Length == 0)
    Console.WriteLine("Please enter a file path to reverse in the console arguments.");
    string p = args[0];
    string o = Path.GetDirectoryName(p) + Path.PathSeparator + Path.GetFileNameWithoutExtension(p) + "Reversed" + Path.GetExtension(p);
     String line;
     Stack< string > lines = new Stack< string >();
     // Create an instance of StreamReader to read from a file.
     // The using statement also closes the StreamReader.
     using (StreamReader sr = new StreamReader(p))
      // Read and display lines from the file until the end of
      // the file is reached.
      while ((line = sr.ReadLine()) != null)
     // create a writer and open the file
     // The using statement also closes the StreamWriter.
     using (TextWriter tw = new StreamWriter(o))
      // write a line of text to the file
      while (lines.Count > 0)
    catch (Exception e)
     // Let the user know what went wrong.
     Console.WriteLine("The file could not be read/written:");

No comments:

Post a Comment