数据可能比较大,而且未知。比如说我要查入的为txt内容为
 姓名  ip 
 李三  178.165.12.0
 章四  196.168.18.1
但是是可变的
要什么函数实现呢
这时我实现第一方法:
         INT ch;
FILE* fp; fp = fopen("meetingnote.txt","r+");
ch = getc(fp);
while (ch!=EOF)
{
test.AddString(_T(fp));   
ch = getc(fp);
}
提示:Error 1 error C2664: 'CStdioFile::Open' : cannot convert parameter 1 from 'const char [9]' to 'LPCTSTR' e:\lianxi\コード\test2\test2\dlg1.cpp 45
我知道fp是指针,而AddString里面要用字符串。但是我不知道要用那个函数才正确,请指明
方法2
CStdioFile   m_File;   
CString   str;   
m_File.Open("d:\test.txt",CStdioFile::modeReadWrite);        
while(m_File.ReadString(str))     
{   
test.AddString(str.GetBuffer(0));     
}   
m_File.Close();                  
CWnd::OnShowWindow(bShow, nStatus); 我确定d:\test.txt是有文件存在的,为什么也不对,高手帮忙啊,在线等……

解决方案 »

  1.   

    d:\test.txt需要改成d:\\test.txt这样
      

  2.   

    这是笔误,改为d:\\test.txt还是一样的错误
    Error 1 error C2664: 'CStdioFile::Open' : cannot convert parameter 1 from 'const char [12]' to 'LPCTSTR' e:\lianxi\コード\test2\test2\dlg1.cpp 45
      

  3.   

    fp = fopen(L"meetingnote.txt",L"r+");
      

  4.   

    VC++应用程序中读取文本数据连接:http://blog.csdn.net/clever101/archive/2006/05/22/749170.aspx
      

  5.   

    还有一个方法就是
    #include <string>
    #include <iostream>
    #include <fstream>
    void GetStrFromTxt(void)
    {
        ifstream if(d:\\test.txt);
        string str;
        CString myStr;
        while(getline(in,str))
    {
        myStr.format("%s",str);// 这里这样将 string型转化为myStr不知对不对,你自己验证一下
        test.AddString(myStr);     
        
    }
    }
      

  6.   

    一段读文本的代码,或许对你有帮助
    m_list_word是CLISTCTRL
    m_article是CEDIT
    m_list_word.ResetContent();
    m_article="";
    UpdateData(FALSE);
    CString filter;
    filter="文本文件(*.txt)|*.txt||";
    CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,filter);
    if(dlg.DoModal()==IDOK)
    {
    m_list_word.ResetContent();//清空列表
    m_article=="";//清空原文内容
    UpdateData(FALSE);
    CString strpath;
    CString readTxt;
    CString words;
    strpath=dlg.GetPathName();
    CStdioFile myfile;
    BOOL bopened=myfile.Open(strpath,CFile::modeRead);
    if(!bopened)
    {
                 MessageBox(strpath+"文件不能打开!");
     return;
    }
    else
    {  
    m_wndStatusBar.SetText("载入文本名称:"+myfile.GetFileName(),2,0);
    //读出单词
    while(myfile.ReadString(readTxt))
    {
    for(int i=0;i<readTxt.GetLength();i++)
    {
    if(int(readTxt.GetAt(i))>=65 && int(readTxt.GetAt(i))<=90 || int(readTxt.GetAt(i))>=97 && int(readTxt.GetAt(i))<=122) 
    if(readTxt.GetAt(i)<=90) 
    words+=CString(int(readTxt.GetAt(i))+32);
    else
        words+=readTxt.GetAt(i);
        else
    {
        if(words!="")
    {
            m_list_word.InsertString(-1,words);
            words="";
    }
    }
    }
    m_list_word.InsertString(-1,words);
    words="";
    m_article+=readTxt+"\r\n";
    UpdateData(FALSE);
    for(int n=0;n<m_list_word.GetCount();n++)
    {
    if(m_list_word.GetTextLen(n)==0) m_list_word.DeleteString(n);
    }
    m_wordSum=m_list_word.GetCount();
    UpdateData(FALSE);