vc中读取一个txt文件,每次读取一段末尾为tab键的字符串,该如何实现???

解决方案 »

  1.   

    先将文件的所有内容读到CString中。然后用Find函数查找\t后,用Left函数进行截取。
      

  2.   

    CFile file;
    file.Open("C:\\aa.txt",CFile::modeRead);
    CString str;
    file.ReadHuge(str.GetBuffer(),file.GetLength());
    int index = str.Find('\t');
    while(index != -1)
    {
        CString subStr = str.Left(index);//subStr就是截取的一段字符串
        str = str.Mid(index+1);
        index = str.Find('t');
    }
    if(str.GetLength() > 0)
        {
              //剩余的str是最后一段字符串
    }
    str.ReleaseBuffer();
      

  3.   

    楼上的,如果文件很大怎么办?要知道CString的上限是65536个字符。简单的办法就是双缓冲,用两个char Buf[??]配合,寻找'\t',然后用指针进行截取
      

  4.   

    CFile file;
    CFileException e; if(!file.Open(fileName, CFile::modeRead, &e ) )
    {
    AfxMessageBox("Can't find index.ini!");
    return;
    }
    char buf = '\0';
    CString str;
    str.Empty(); file.Read(&buf, 1);
    for(int i=0; i<file.GetLength(); i++)
    {
    if(buf != '\t')
    {
    CString temp;
    temp = buf;
    str =str + temp;
    temp.Empty();
    }
    else
    {
    int j = 0;
    str = str + '\t';
                               //此时已经得到末尾为TAB键的一个字符串
    }
    file.Read(&buf, 1);
    }
    file.Close();这个方法有点笨,而且没有考虑换行
      

  5.   

    #include <iostream>
    #include <fstream>
    using namespace std;ifstream ifile("temp.txt");
    char linebuf[512];
    while(!ifile.eof())
    {
    ifile.get(linebuf, sizeof(linebuf), '\t');
    ifile.get(); //跳过'\t';
    }
    ifile.close();
      

  6.   

    ////////////////////////////////////////////////////////////////char s[255];
    FILE *stream;
    stream = fopen("c:\\YDQ.txt", "r");
    if (stream != NULL)
    {
    while (!feof(stream))
    {
    fscanf( stream, "%s\t", s );
    AfxMessageBox(s);
    }
    fclose(stream);
    }