开发者

C# streamreader, delimiter.

What I have is a txt file that is huge, 60MB. I need to read each line and produce a file, split based on a delimiter. I'm having no issue reading the file or producing the file, my complication comes from the delimiter, it can't see the delimiter. If anybody could offer a suggestion on how to read that delimiter I would be so grateful.

delimiter = Ç

public void file1()
{
    string betaFilePath = @"C:\dtable.txt";
    StringBuilder sb = new StringBuilder();
    using (FileStream fs = new FileStream(betaFilePath, FileMode.Open))
    using (StreamReader rdr = new StreamReader(fs))
    {
        while (!rdr.EndOfStream)
        {
            string[] betaFileLine = rdr.ReadLine().Split('Ç');
            {
 开发者_Go百科               sb.AppendLine(betaFileLine[0] + "ç" + betaFileLine[1] + betaFileLine[2] + "ç" + betaFileLine[3] + "ç" + betaFileLine[4] + "ç" + betaFileLine[5] + "ç" + betaFileLine[6] + "ç" + betaFileLine[7] + "ç" + betaFileLine[8] + "ç" + betaFileLine[9] + "ç" + betaFileLine[10] + "ç");
            }
        }
    }
    using (FileStream fs = new FileStream(@"C:\testarea\load1.txt", FileMode.Create))
    using (StreamWriter writer = new StreamWriter(fs))
    {
        writer.Write(sb.ToString());
    }
}

Small update:

string[] betaFileLine = rdr.ReadLine().Split('Ç'); 

It's not able to split on the charecter Ç. When i debug it, it comes out to be an unreadable charecter.

Guys, thanks for the help you defiently pointed my in the right direction.

Changing:

using (StreamReader rdr = new StreamReader(fs))  

To:

using (StreamReader rdr = new StreamReader((fs),Encoding.Default))  

Fixed it.


Make sure your StreamReader uses the same text encoding as your text file. The constructor can take a second parameter to indicate the encoding to use.


It looks like you are splitting on Ç but adding ç. Not something as simple as replacing the uppercase delimiter with the lowercase or vice versa.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜