如何通过1 C#在一个文件中读出1字 [英] How to read character in a file 1 by 1 c#

查看:151
本文介绍了如何通过1 C#在一个文件中读出1字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的程序中的所有字符1:1和徘徊无论它找到支持引号(),它补充说,倒逗号之前分号读取文本文件如:我们有一个文本文件如下段落:

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 paragraph in a text file as follow:

这是conains很多很多人物和一些
的名字和日期的段落。我的名字叫山姆我出生在12:00,正午我住在
任何地方。

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 :

这是conains很多很多人物和一些
的名字和日期的段落我的名字。 萨姆,我出生在;12:00;;中午,我
住。任何地方;

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;" .

应该再使用文件流中读取字符,然后将分号它认定倒comman打开该文件,而且输出应该是等于textbox1.text。

It should open the file using file stream then reads character and then adds semicolon where it finds inverted comman. And the output should be equals to textbox1.text.

CODE,直到我发了言: -

CODE TILL I MADE: -

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();
        }
    }
}



OUTPUT: -

OUTPUT:-

这是conains很多很多人物和一些
的名字和日期的段落。我的名字叫山姆;我出生在12:00; ;中午;。我
住在任何地方; 。 154个字符

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 inverted comma. any help would be appreciated. Thanks

推荐答案

交换的操作顺序:

    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.

这篇关于如何通过1 C#在一个文件中读出1字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆