How to read character in a file 1 by 1 c#

前端 未结 4 520
夕颜
夕颜 2021-01-05 05:34

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

相关标签:
4条回答
  • 2021-01-05 06:12

    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 ("\"", ";\""));
    
    0 讨论(0)
  • 2021-01-05 06:22

    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(@";")
    
    0 讨论(0)
  • 2021-01-05 06:25

    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.

    0 讨论(0)
  • 2021-01-05 06:31
    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);
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题