I want my program to read a text file all characters 1 by 1 and whereever it finds inverted comma ( \" ), it adds a semicolon before that inverted comma. For eg we have a pa
Do you have to read in character by character? The following code will do the whole thing as a block and return you a list containing all your lines.
var contents = File.ReadAllLines (@"C:\Users\user1\Documents\data.txt")
.Select (l => l.Replace ("\"", ";\""));
Try ch = (char)reader.Peek();
This will read tell you the next character without reading it. You can then use this to check if it is a " or not an insert : accordingly
if (Convert.ToInt32((char)read.Peek()) == 34) Console.Write(@";")
Swap the order of the operations:
if (Convert.ToInt32(ch) == 34)
{
Console.Write(@";");
}
Console.Write(ch);
e.g. don't write the original character until AFTER you've decided to output a semicolon or not.
using System;
using System.IO;
using System.Text;
namespace getto
{
class Program
{
static void Main(string[] args)
{
var path = @"C:\Users\VASANTH14122018\Desktop\file.v";
string content = File.ReadAllText(path, Encoding.UTF8);
Console.WriteLine(content);
//string helloWorld = "Hello, world!";
foreach(char c in content)
{
Console.WriteLine(c);
}
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}