1. Change the value of "0" in the input file to "1" and the value of "1" to "0" and write to the output file
using System;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// input file: fill the file 'in.txt' with data (in folder bin\debug)
const string fileNameIn = "in.txt";
// output file
const string fileNameOut = "out.txt";
try
{
// read data from file
string fileContentsIn = System.IO.File.ReadAllText(fileNameIn);
StringBuilder fileContentsOut = new StringBuilder(fileContentsIn);
// replace "0" to "1" and "1" to "0"
for (int i = 0; i < fileContentsOut.Length; i++)
{
if (fileContentsOut[i] == '0')
{
fileContentsOut[i] = '1';
}
else if (fileContentsOut[i] == '1')
{
fileContentsOut[i] = '0';
}
}
// write output file
System.IO.File.WriteAllText(fileNameOut, fileContentsOut.ToString());
}
catch (Exception e)
{
Console.WriteLine("Error:" + e.Message);
return;
}
}
}
}
Comments
Leave a comment