c# - 如何1乘1读取文件中的字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16618341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read character in a file 1 by 1 c#
提问by Sam
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:
我希望我的程序读取一个文本文件中的所有字符 1 x 1,无论它在哪里找到倒逗号( " ),它都会在倒逗号之前添加一个分号。例如,我们在文本文件中有一个段落,如下所示:
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" .
这是一个包含大量字符以及一些名称和日期的段落。我的名字“山姆”我出生于“12:00”“中午”。我住在“任何地方”。
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;" .
这是一个包含大量字符以及一些名称和日期的段落。我的名字;“山姆;” 我出生于 ;"12:00;" ;“中午;”。我住在;"任何地方;" .
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.
它应该使用文件流打开文件,然后读取字符,然后在找到倒置命令的地方添加分号。并且输出应该等于 textbox1.text。
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:-
输出:-
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
这是一个包含大量字符以及一些名称和日期的段落。我的名字“;山姆”;我出生于 ";12:00"; “;中午”;。我住在“;任何地方”; . 154 个字符
I want that semicolon before the inverted comma. any help would be appreciated. Thanks
我想要在倒逗号之前的分号。任何帮助,将不胜感激。谢谢
采纳答案by Marc B
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.
例如,在您决定是否输出分号之前不要写原始字符。
回答by SkeetJon
Try ch = (char)reader.Peek();
尝试 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(@";")
回答by Andrew
Do you haveto 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 ("\"", ";\""));

![C# 如何使用streamreader以当前编码读取字节[]](/res/img/loading.gif)