10

I want my program to read a text file all characters 1 by 1 and whereever it finds a double-quote ("), it adds a semicolon before that inverted comma. For eg we have a paragraph in a text file as follow:

This is a paragraph which conains lots and lots of characters and some names and dates. My name "Sam" i was born at "12:00" "noon". I live in "anyplace" .

Now I want the output to be as follows:

This is a paragraph which conains lots and lots of characters and some names and dates. My name ;"Sam;" i was born at ;"12:00;" ;"noon;". I live in ;"anyplace;" .

It should open the file using file stream then reads character and then adds semicolon where it finds quotes. And the output should be equal to textbox1.Text.

This is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            
            char ch;
            int Tchar = 0;
            StreamReader reader;
            reader = new StreamReader(@"C:\Users\user1\Documents\data.txt");
            do
            {
                ch = (char)reader.Read();
                Console.Write(ch);
                if (Convert.ToInt32(ch) == 34)
                {
                    Console.Write(@";");
                }
                Tchar++;
            } while (!reader.EndOfStream);
            reader.Close();
            reader.Dispose();
            Console.WriteLine(" ");
            Console.WriteLine(Tchar.ToString() + " characters");
            Console.ReadLine();
        }
    }
}

This is the output:

This is a paragraph which conains lots and lots of characters and some names and dates. My name ";Sam"; i was born at ";12:00"; ";noon";. I live in ";anyplace"; . 154 characters

I want that semicolon before the quotes. Any help would be appreciated. Thanks!

dimitar.bogdanov
  • 387
  • 2
  • 10
Sam
  • 105
  • 1
  • 1
  • 6

4 Answers4

7

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • i feel stupid. I thought with this we can't add semicolon if first character is ( " ), thanks for the answer – Sam May 17 '13 at 21:55
1

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(@";")
SkeetJon
  • 1,491
  • 1
  • 19
  • 40
1

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 ("\"", ";\""));
Andrew
  • 2,315
  • 3
  • 27
  • 42
-1
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);
    }
  }
}