Search code examples
c#streamreader

Replace multiple strings in text files with different texts


I have a text file like so:

template.txt

hello my name is [MYNAME], and i am of age [AGE].
i live in [COUNTRY].
i love to eat [FOOD]

and I am trying to replace whatever is in the square brackets with strings from a list example

//         // name //country // age // food           
p.Add(new Person("jack", "NZ", "20", "Prawns"));
p.Add(new Person("ana", "AUS", "23", "Chicken"));
p.Add(new Person("tom", "USA", "30", "Lamb"));
p.Add(new Person("ken", "JAPAN", "15", "Candy"));

so far I have tried the below function which I call inside a loop

//loop
 static void Main(string[] args)
{
   int count = 0;
  foreach (var l in p)
  {
    FindAndReplace("template.txt","output"+count+".txt" ,"[MYNAME]",l.name);
    FindAndReplace("template.txt","output"+count+".txt" ,"[COUNTRY]",l.country);
    FindAndReplace("template.txt","output"+count+".txt" ,"[AGE]",l.age);
    FindAndReplace("template.txt","output"+count+".txt" ,"[FOOD]",l.food);
    count++;
  }
}
//find and replace function
 private static void FindAndReplace(string template_path,string save_path,string find,string replace)
        {           
            using (var sourceFile = File.OpenText(template_path))
            {
                // Open a stream for the temporary file
                using (var tempFileStream = new StreamWriter(save_path))
                {
                    string line;
                    // read lines while the file has them
                    while ((line = sourceFile.ReadLine()) != null)
                    {
                        // Do the word replacement
                        line = line.Replace(find, replace);
                        // Write the modified line to the new file
                        tempFileStream.WriteLine(line);
                    }
                }
            }
  
        }

this is what I have done. But the output I get is this

output1.txt

hello my name is [MYNAME], and i am of age [AGE].
i live in [COUNTRY].
i love to eat Prawns

output2.txt

hello my name is [MYNAME], and i am of age [AGE].
i live in [COUNTRY].
i love to eat Chicken

Only the last text is replaced.


Solution

  • instead of a function try doing something like this

    static void Main(string[] args)
    {
                   int count = 0;
                    foreach (var l in p)
                    {                  
                        using (var sourceFile = File.OpenText("template.txt"))
                        {
                            // Open a stream for the temporary file
                            using (var tempFileStream = new StreamWriter("output" + count + ".txt"))
                            {
                                string line;
                                // read lines while the file has them
                                while ((line = sourceFile.ReadLine()) != null)
                                {
                                   
                                    line = line.Replace("[MYNAME]", l.name);
                                    line = line.Replace("[COUNTRY]", l.country);
                                    line = line.Replace("[AGE]", l.age);
                                    line = line.Replace("[FOOD]", l.food);
                                    tempFileStream.WriteLine(line);
                                }// end of while loop
                            }
                          count++;
                        }//end foreach loop                             
                    }
                }//end of main