I'm trying to solve this question at the moment:
Write a program that replaces every occurrence of the substring "start" with "finish" in a text file. Can you rewrite the program to replace whole words only? Does the program work for large files (e.g. 800 MB)?
I've been trying to do it but apparently you cant read and write at the same time. If someone could look at my code and help me it would be awesome. It's throwing an exception:
The process cannot access the file 'C:\Users\Nate\Documents\Visual Studio 2015\Projects\Chapter 15\Chapter 15 Question 7\Chapter 15 Question 7\TextFile.txt' because it is being used by another process.
You dont have to give me the answer straight but rather tell me the process. Thanks!
Here's my code at the moment
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Chapter_15_Question_7
{
class Program
{
static void Main(string[] args)
{
StreamReader reader = new StreamReader(
@"C:\Users\Nate\Documents\Visual Studio 2015\Projects\Chapter 15\Chapter 15 Question 7\Chapter 15 Question 7\TextFile.txt");
StreamWriter writer = new StreamWriter(
@"C:\Users\Nate\Documents\Visual Studio 2015\Projects\Chapter 15\Chapter 15 Question 7\Chapter 15 Question 7\TextFile.txt");
using (writer)
{
using (reader)
{
string line = reader.ReadLine();
while (line != null)
{
line.Replace("start", "finish");
writer.WriteLine(line);
line = reader.ReadLine();
}
}
}
}
}
}