我的txt文件是一行一行的,每一行是一个词条,现在想把txt读进来,按行存储这些词条,应该怎么读?存储的时候用什么最好,字符数组?我是想用已知的词条去和txt文件中的词条一一进行比对,找相符的。这样的话读进来这些词条用什么存储好?

解决方案 »

  1.   

    是用CStdioFile类吗?这个怎么用?谁能给我讲讲,我查的书上讲的不是很细,还是不会。
      

  2.   

    fgetline(fp, buffer, maxsize_buffer);
    每次从文件往buffer里读入maxsize_buffer个字符。你可以将maxsize_buffer设置成一行的大小。
      

  3.   

    CArchive CFile ReadString
    不过上面的方法只支持ANSI编码的txt文件。
      

  4.   

    不好意思,说错了,用CArchive的方法,如果是MBCS版的,就只支持ANSI编码的txt文件,如果是Unicode版,就只支持Unicode编码的txt文件。
      

  5.   

    void Read3dDataEachLine(const char* szFileName = "test.txt")
    {
    if(szFileName == NULL)
    return; char FullPath[100] = {"\0"};
    ::GetModuleFileName(NULL,FullPath,100);
    PathRemoveFileSpec(FullPath);
    string File = FullPath;
    File += "\\";
    File += szFileName; FILE* pFile = fopen(File.c_str(),"r");
    if (NULL == pFile)
    {
    cout<<"读文件出错!"<<endl;
    return;
    } char szLine[100] = {0};
    char cData;
    int nIndex = 0; while (fread(&cData,1,1,pFile) > 0)
    {
    if (cData == '\n')
    {
    ProcessEachLine(szLine);//对每行进行处理 strcpy(szLine,"\0");
    nIndex = 0;
    }
    else
    {
    szLine[nIndex] = cData;
    nIndex++;
    }
    }
    }
      

  6.   

    #include <string>
    #include <iostream>
    #include <vector>
    #include <fstream>
    using namespace std;//read file
    vector<string> s;
    string strtmp;
    ifstream infile("123.txt");
    while ( infile >> strtmp )
    {
    s.push_back( strtmp );
    }//find
    string sc="11"; vector<string>::iterator it;
    for(it = s.begin(); it != s.end(); ++it)
    {
    string stmp = *it;
    int ifind = stmp.find(sc, 0);
    if(0 == ifind)
    cout << *it << '\n';
    }
      

  7.   

    void ReadFromFile(const CString& strFile)
    {
    if(strFile.IsEmpty())
    {
    AfxMessageBox(_T("Invaild Path!"));
    return;
    }
    CStdioFile file; if(file.Open(strFile,CFile::modeRead|CFile::typeText   ) )
    {
    CString str = _T("");
    while (file.ReadString(str))
    {
                         m_vector.push_back(str);//m_vector为vector<CString>容器.
    }

    }
    }