using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;namespace ReadFile
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 1, m;                                                                //变量声明
            byte[] array = new byte[2];
            byte[] byData = new byte[1];
            char[] charData = new char[100];
            string Filename,ss;                                                          //输入文件名
            Console.WriteLine("请输入文件名,按回车输入完毕");
            Filename=Console.ReadLine();
           
            Encoding gb = Encoding.GetEncoding("gb2312");
           
            try
            {
                FileStream aFile = new FileStream(Filename, FileMode.Open);             //打开文件
                
                for (i = 1; aFile.Length!= aFile.Position; i++)                         //循环操作 读取字符 直至文件尾
                {
                    aFile.Read(byData, 0, 1);                                           //读取一个字符 
                    Decoder f = Encoding.UTF8.GetDecoder();
                    f.GetChars(byData, 0, 1, charData, 0);
            
                    if (charData[0] == ':')                                             //找冒号 找到后输出其前面的字符
                    {
                        i = i - 2;                        aFile.Seek(i, SeekOrigin.Begin);         //指针移至冒号前面的字符
                        aFile.Read(byData, 0, 1);                //读取
                        aFile.Seek(i + 2, SeekOrigin.Begin);     //指针复位
                        Decoder e = Encoding.UTF8.GetDecoder();
                        e.GetChars(byData, 0, byData.Length, charData, 0);//将读取的字符 转到字符数组
                        i = i + 2;                                        //i复位
                        Console.WriteLine(charData);                      //输出字符                        if (((charData[0] > 'A') && (charData[0] < 'z')) || (('0' < charData[0]) && (charData[0] < '9')))
                                                 {
                                                     Console.WriteLine(charData);                                                 }
                                                 else
                                                 {
                                                     i = i - 3;
                                                     aFile.Seek(i, SeekOrigin.Begin);
                                                     aFile.Read(byData, 0, 2);
                                                     Decoder d = Encoding.UTF8.GetDecoder();
                                                     d.GetChars(byData, 0, byData.Length, charData, 0);
                                                  
                                                     aFile.Seek(i + 3, SeekOrigin.Begin);                                                   
                                                     Console.WriteLine(charData);
                                                     i = i + 3;
                                                 }
                                         }
                }
               
            }
            catch (IOException e)                                   //异常处理
            {
                Console.WriteLine("An IO exception has been thrown!");
                Console.WriteLine(e.ToString());
                Console.ReadLine();
                return;
            }
            
        
         }
    }
}大体意思是输出文件中冒号前的字符,但是如果是汉字会出错 ,求人解决

解决方案 »

  1.   

    搂主读个文件怎么用这么麻烦,且容易出错的方法?
    我刚给你写了一个,参考一下:
    static void Main(string[] args)
    {
        StreamReader reader = new StreamReader(filePath, Encoding.Default);
        string line = string.Empty;
        while ((line = reader.ReadLine()) != null)
        {
            int pos = line.IndexOf(':');
            if (pos >= 0)
            {
                Console.WriteLine(line.Substring(++pos));
            }
        }
        reader.Close();
        reader.Dispose();
        Console.ReadKey();
    }
      

  2.   

    就是啊,好麻烦,C#字符处理函数IndexOf(),IndexOfAny(),LastIndexOf(),LastIndexOfAny()中的任何一个函数都可以实现你的目的啊
      

  3.   

    楼主以前可能是用c编程,找到冒号之后截取之前的字符,c#中的String类很强大
      

  4.   

    噢,是前面的呀?那改下:
    static void Main(string[] args)
    {
        StreamReader reader = new StreamReader(filePath, Encoding.Default);
        string line = string.Empty;
        while ((line = reader.ReadLine()) != null)
        {
            int pos = line.IndexOf(':');
            if (pos > 0)
            {
                Console.WriteLine(line.Substring(0,pos));
            }
        }
        reader.Close();
        reader.Dispose();
        Console.ReadKey();
    }如果搂主只是想取出汉字,而不要其他字符的话,还得循环判断下ASCII码