怎样从一个.txt文件中读入中文的文本?
渴望代码!

解决方案 »

  1.   

    CFile myfile;
    myfile.Open("your.txt",CFile::modeRead);
    char txtbuffer[2048];
    myfile.Read(txtbuffer,2048);txtbuffer即是读入的文本。英文和中文均可。
      

  2.   

    CStdioFile file;
    CString str;
    file.Open("文件名.txt",CFile::modeRead);
    file.ReadString(str);
    file.Close;
    MessageBox(str);
      

  3.   

    不会~
    ReadString每读一次都是遇到'\n'才停
      

  4.   

    重新给你一段代码。
    CFile myfile;
    myfile.Open("your.txt",CFile::modeRead);
    CString txtbuffer;
    int filelength = myfile.GetLength();
    myfile.Read(txtbuffer,filelength);
    myfile.Close;
    txtbuffer即是读入的文本.这样保证不会有截断的现象。实际上, shilong(银羽(★★倒计时250分)) 已经很完美了。
      

  5.   

    我刚刚编了一个中文的搜所算法,建议如果你对中文进行编辑,查找的话,最好还是用
    UNICODE编码.see more about
    wchar_t,and BSTR
    and it is not so hard!
      

  6.   

    int LoadChTxt(char *str)
    {
    CFile myfile;
        myfile.Open("data/document.txt",CFile::modeRead);
        char txtbuffer[2048];
        myfile.Read(txtbuffer,2048);
    str=txtbuffer;
    return 1;
    }
    int CMyBmpDlgDlg::OnButton1() 
    {

    CAboutDlg Dlg;
    Dlg.DoModal();
    char ttt[2084];
            if(!LoadChTxt(ttt))
                 return 0;
    GetDlgItem(IDC_STATIC)->SetWindowText(ttt);
    Invalidate();
              return 1;
    }
      

  7.   

    int LoadChTxt(char *str)
    {
    CFile myfile;
        myfile.Open("data/document.txt",CFile::modeRead);
            myfile.Read(str,2048);
      return 1;
    }
      

  8.   

    或者
    int LoadChTxt(char *str)
    {
    CFile myfile;
        myfile.Open("data/document.txt",CFile::modeRead);
        char txtbuffer[2048];
        myfile.Read(txtbuffer,2048);
    strcpy(str,txtbuffer);
    return 1;
    }
      

  9.   

    OK了,不过这什么加了str=txtbuffer就不行了呢?它们不是同一个类型吗?
      

  10.   


    str=txtbuffer
    把str指向txtbuffer[2048]这段内存而char txtbuffer[2048];
    是局部变量,当函数结束时,它就无郊了
      

  11.   

    //use C++ iostream         char *chError = (char *)new(char[50]);
    ifstream input;
    int cStrBuffer[1]; //read_buffer input.open(chFile1,ios::in);//the second parameter means istream
    if(input.fail())
    {
    chError = "Error in opening the file:";
    strcat(chError,chFile1);
    AfxMessageBox(chError);
    exit(1);
    }
             input.read((unsigned char *)&cStrBuffer,1);//the second parameter means 
    //how many bytes you want to read
      

  12.   

    教你一绝活:
        用系统映象文件的方法读取这个文件,映象文件初始化后,你可以得到指向文件头的
    一个指针,有了指针,操作这个文件就可以象操作内存数据一样了。    接下来一个字符一个字符读取,遇到字符值大于128,则判断这肯定是一个汉字的前
    半部分,所以我们读取二个字符,这样就把一个汉字读取了。然后NEXT,判断下一个字符
    的值。