none
c# Move every third line in the .txtFile on the end of the line that is above that line... RRS feed

  • Question

  • Hello everyone,
    I would like to move every third line in the .txtFile on the end of the line that is above that line.

    For example:

    If my .txtFile contains (before executing the code):

    First line
    Second line
    Third line
    Fourth line
    Fifth line
    Sixth line
    Seventh line
    Eighth line
    Ninth line
    Tenth line
    Eleventh line
    Twelfth line

    After executing the code

    First line
    Second lineThird line
    Fourth line
    Fifth lineSixth line
    Seventh line
    Eighth lineNinth line
    Tenth line
    Eleventh lineTwelfth line

    Note:
    txt.File has about 200 lines. Thank you.
    Tuesday, April 10, 2018 9:03 AM

Answers

  • Every time logic is same very little changes are there

    class Program
        {
            static void Main(string[] args)
            {
                createOutput();
                Console.ReadKey();
            }
    
            private static void createOutput()
            {
                string newFilePath = @"C:\temp\file1.txt";
                string[] s1 = File.ReadLines(newFilePath).ToArray<string>();
                if (File.Exists(newFilePath))
                {
                    File.Delete(newFilePath);
                }
                File.Create(newFilePath).Dispose();
                TextWriter txtWrt = new StreamWriter(newFilePath);
                for (int i = 0; i < s1.Length; i += 3)
                {
                    txtWrt.WriteLine(s1[i]);
                    txtWrt.WriteLine(s1[i + 1] + s1[i + 2]);
                }
                txtWrt.Close();
                Console.WriteLine("Output File Created.");
            }
        }

    • Marked as answer by ZoAndrea Tuesday, April 10, 2018 4:09 PM
    Tuesday, April 10, 2018 12:56 PM
  • Hello ZoAndrea,

    Also you could refer the example I have wrote before.

    static void Main(string[] args)
            {
    
                string filePath = "../../TextFile1.txt";
                var lines = File.ReadAllLines(filePath);
    
                int index = 0;
                var newLines = lines.Aggregate("", (all, cur) =>
                 {
                     index++;
                     if (index % 3 == 2)
                         return all + cur;
                     else
                         return all + cur + Environment.NewLine;
                 });
                File.WriteAllText(filePath, newLines);
            }

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ZoAndrea Wednesday, April 11, 2018 6:29 AM
    Wednesday, April 11, 2018 6:17 AM
    Moderator

All replies

  • What did your teacher told you as advice?

    Success
    Cor


    Tuesday, April 10, 2018 9:06 AM
  • No, there is no teacher, it makes it easier for me to work every day.
    Tuesday, April 10, 2018 9:56 AM
  • But the fun is, to make this, it contains in a short way some typical challenges for students. 

    Can you show what you did so far?


    Success
    Cor

    Tuesday, April 10, 2018 10:29 AM
  • https://social.msdn.microsoft.com/Forums/vstudio/en-US/81a6d3ed-17c8-45a0-87bf-0e09f68ee052/how-to-combine-three-txt-files-into-one-txt-file-using-c?forum=csharpgeneral

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/b5435182-6d65-4caa-a469-df1f02c6b5f1/c-add-text-from-textbox1text-at-the-beginning-of-the-every-fourth-lines-to-txt-file?forum=csharpgeneral
    Tuesday, April 10, 2018 10:52 AM
  • You mean you did only copied and pastes answers you got in this forum?

    How many of those files do you have, because it is easy to do using a texteditor and probably even quicker then using a program.

    Or you should of course find a fool who does it for you.


    Success
    Cor


    Tuesday, April 10, 2018 11:23 AM
  • Dear Cor,
    Please do not help me anymore.
    Thank you.
    Tuesday, April 10, 2018 12:01 PM
  • Every time logic is same very little changes are there

    class Program
        {
            static void Main(string[] args)
            {
                createOutput();
                Console.ReadKey();
            }
    
            private static void createOutput()
            {
                string newFilePath = @"C:\temp\file1.txt";
                string[] s1 = File.ReadLines(newFilePath).ToArray<string>();
                if (File.Exists(newFilePath))
                {
                    File.Delete(newFilePath);
                }
                File.Create(newFilePath).Dispose();
                TextWriter txtWrt = new StreamWriter(newFilePath);
                for (int i = 0; i < s1.Length; i += 3)
                {
                    txtWrt.WriteLine(s1[i]);
                    txtWrt.WriteLine(s1[i + 1] + s1[i + 2]);
                }
                txtWrt.Close();
                Console.WriteLine("Output File Created.");
            }
        }

    • Marked as answer by ZoAndrea Tuesday, April 10, 2018 4:09 PM
    Tuesday, April 10, 2018 12:56 PM
  • I'm not helping you. I try to warn others not to be let them used because you use a nick with a female name in it. Simply looking at the history of your question shows what you are doing.


    Success
    Cor



    Tuesday, April 10, 2018 1:01 PM
  • Thank you Vikram a lot. You've helped me many times.
    Your code works fine, I needed it.
    Also thank you very much for Fei Hu. Your code works great.
    Thanks again.

    • Edited by ZoAndrea Wednesday, April 11, 2018 6:31 AM
    Tuesday, April 10, 2018 4:09 PM
  • Hello ZoAndrea,

    Also you could refer the example I have wrote before.

    static void Main(string[] args)
            {
    
                string filePath = "../../TextFile1.txt";
                var lines = File.ReadAllLines(filePath);
    
                int index = 0;
                var newLines = lines.Aggregate("", (all, cur) =>
                 {
                     index++;
                     if (index % 3 == 2)
                         return all + cur;
                     else
                         return all + cur + Environment.NewLine;
                 });
                File.WriteAllText(filePath, newLines);
            }

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ZoAndrea Wednesday, April 11, 2018 6:29 AM
    Wednesday, April 11, 2018 6:17 AM
    Moderator
  • Every time logic is same very little changes are there

    class Program
        {
            static void Main(string[] args)
            {
                createOutput();
                Console.ReadKey();
            }
    
            private static void createOutput()
            {
                string newFilePath = @"C:\temp\file1.txt";
                string[] s1 = File.ReadLines(newFilePath).ToArray<string>();
                if (File.Exists(newFilePath))
                {
                    File.Delete(newFilePath);
                }
                File.Create(newFilePath).Dispose();
                TextWriter txtWrt = new StreamWriter(newFilePath);
                for (int i = 0; i < s1.Length; i += 3)
                {
                    txtWrt.WriteLine(s1[i]);
                    txtWrt.WriteLine(s1[i + 1] + s1[i + 2]);
                }
                txtWrt.Close();
                Console.WriteLine("Output File Created.");
            }
        }

    This answer will throw "System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'" if the lines number were not multiple of 3
    Wednesday, April 11, 2018 6:46 AM