Proposed exercise
Create a program to replace words in a text file, saving the result into a new file.
The file, the word to search and word to replace it with must be given as parameters:
replace file.txt hello goodbye
The new file would be named "file.txt.out" and contain all the appearances of "hello" replaced by "goodbye".
Output
Solution
using System; using System.IO; class TextReplacer { static void Main() { ReplaceText("data.data", "Hello", "hello"); } public static void ReplaceText(string name, string text, string newText) { try { StreamReader file = File.OpenText(name); StreamWriter fileReplace = File.CreateText("replace.data"); string line = " "; do { line = file.ReadLine(); if (line != null) { line = line.Replace(text, newText); fileReplace.WriteLine(line); } } while (line != null); fileReplace.Close(); file.Close(); } catch (Exception) { Console.WriteLine("Error!!!"); } } }