Chinaunix首页 | 论坛 | 博客
  • 博客访问: 183091
  • 博文数量: 27
  • 博客积分: 2510
  • 博客等级: 少校
  • 技术积分: 285
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-20 10:21
个人简介

6年的blog,重新拾起,再来耕耘!

文章分类

全部博文(27)

文章存档

2013年(1)

2009年(9)

2007年(17)

我的朋友

分类: C/C++

2007-12-10 16:52:34

FILE *f = fopen("C:\\1.txt","rb");
    if(f)
    {
        unsigned char hdr[2];
        fread(hdr, 1, 2, f);//读UNICODE文本文件头2个字节

        if(hdr[0]==0xff && hdr[1]==0xfe)//是UNICODE文本文件

        {
            wchar_t    wsLine[1024];
            std::vector <CString> fds;

            fds.clear();

            while(fgetws(wsLine,1024,f))//按行读取

            {
                wchar_t *p,*s = wsLine;
                bool temp = false;
                for(p=s; *p; p++)
                {
                    if(*p==' '||*p=='\t')
                    {
                        *p = 0;
                        if (temp)    fds.push_back(s);
                        s = p+1;
                        temp = false;

                    }
                    else if(*p=='\r'||*p=='\n') // 行末

                    {
                        *p = 0;
                        fds.push_back(s);
                        s = p;
                        break;
                    }
                    else
                        temp = true;
                }
                if(*s) // 最后一行可能没有回车,这里判断一下

                {
                    fds.push_back(s);
                }
                
            }
                int nCount = fds.size(); // 这一行一共有的字段个数

                for(int i=0; i <nCount; i++)
                {
                    CString ws = fds[i]; // 这一行的第 i 个字段的值

                    AfxMessageBox(ws);
                }
            //}

        }
        else
        {
            AfxMessageBox("not unicode");
        }
        fclose(f);
    }
    else
    {
        AfxMessageBox("error");
    }

阅读(2665) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~