0

I have a code that:

  • reads a text file,
  • replaces all "spaces" with ";",
  • removes the repetition of the character ";"
  • and saves a new file with all changes.

But instead of writing all the rows in the file, it writes only the last row.

static void Main(string[] args)
    {
        RegexOptions options = RegexOptions.None;
        Regex regex = new Regex("[ ]{2,}", options);
        
        string FilePath = @"C:\Users\User\Desktop\download1.txt";
        string OutputhFilePath = @"C:\Users\User\Desktop\download2.txt";

        List<string> lines = new List<string>();
        lines = File.ReadAllLines(FilePath).ToList();
        
        foreach (string line in lines)
        {
            string line1 =  line.Replace(' ', ';');
            line1 = regex.Replace(line, ";");
            Console.WriteLine(line1);
            File.WriteAllText(OutputhFilePath, line1);
        }
        
            Console.ReadLine();
    }

Text file:

Text file after code:

Can someone help me with these? Thanks in advance!

Alex27
  • 87
  • 10
  • 2
    Could you imagine what ```WriteAllText()``` does just by the name ? The first sentence of https://learn.microsoft.com/en-us/dotnet/api/system.io.file.writealltext?view=netcore-3.1 may help. Maybe you look for https://learn.microsoft.com/en-us/dotnet/api/system.io.file.appendalltext?view=netcore-3.1 – nanu_nana Jun 29 '20 at 09:24
  • You init line1 by `string line1 = line.Replace(' ', ';');` and then you overwriting this value by `line1 = regex.Replace(line, ";");`. Is it expected? – Leszek Mazur Jun 29 '20 at 09:28

3 Answers3

3

The File.WriteAllText Method do:

Creates a new file, write the contents to the file, and then closes the file. If the target file already exists, it is overwritten.

So you overwriting file in each line readed... You may use StringBuilder to do it:

static void Main(string[] args)
{
    RegexOptions options = RegexOptions.None;
    Regex regex = new Regex("[ ]{2,}", options);
    
    string FilePath = @"C:\Users\User\Desktop\download1.txt";
    string OutputhFilePath = @"C:\Users\User\Desktop\download2.txt";

    List<string> lines = new List<string>();
    lines = File.ReadAllLines(FilePath).ToList();
    var sb = new StringBuilder();

    foreach (string line in lines)
    {
        string line1 =  line.Replace(' ', ';');
        line1 = regex.Replace(line, ";");
        Console.WriteLine(line1);
        sb.AppendLine(line1);
    }
    
    File.WriteAllText(OutputhFilePath, sb.ToString());
    Console.ReadLine();
}
Leszek Mazur
  • 2,443
  • 1
  • 14
  • 28
1

You need File.AppendAllText().

You're overwriting each previous line with the new one, which is why you're only seeing the last input line.

InteXX
  • 6,135
  • 6
  • 43
  • 80
1

The problem is you are outputting to the file after each line. What File.WriteAllText does is it always overwrites the target file with the latest data. What you need instead is to append to the file. Try for example the File.AppendText method.

However an even better solution is to use a StringBuilder, append all lines to it and then output the whole content (builder.ToString()) at once. This avoids excessive opening and closing of the file after each line.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91