CFile file;
long lposition=0;
CString str;
CFileException e;
if( !file.Open( "test",CFile::modeRead, &e ) )
   {
#ifdef _DEBUG
   afxDump << "File could not be opened " << e.m_cause << "\n";
#endif
   }
   CString s[100];
   int itemp=0;
   long remeber=0;
   while(1){
file.Seek(lposition, CFile::begin );
    file.Read(str.GetBuffer(str.GetLength()),1);
if(*str==EOF){
break;
}
    if(str=="\n"){
file.Seek(remeber, CFile::begin );
file.Read(str.GetBuffer(str.GetLength()),lposition-remeber);
remeber=lposition+1;
s[i]=str;

}   
lposition++;

   }
   file.Close();
//读test文件,放入s[]中。
test文件。
abcdefg
abcdef1
abcdef2
abcdef3
----------
期望结果:s[0]=abcdefg a[1]=abcde1.......

解决方案 »

  1.   

    if( !file.Open( "test",CFile::modeRead, &e ) ):这局不对文件名没有扩展名
      

  2.   

    不好意思,把int itemp=0;改为long remeber=0;
      

  3.   

    用CStdiofile 他是CFile的子类
    例如:
    CStdioFile file;
    file.Open("test.txt",CFile::modeRead);
    file.ReadString(str);//读第一行
    file.ReadString(str);//读第二行
    file.CLose();
      

  4.   

    麻烦大虾帮我debug一下,看看,内存里的状况,好奇怪,能给我讲讲为什么吗?
      

  5.   

    Pipi0714(皮皮0714) 谢谢你,我不太想用别的方法(用fopen之类的都可以),因为这个方法不通,让我很不爽,除非这个方法是不可行的......但我想知道理由。
      

  6.   

    太烂了,还是重新写吧。
    FILE *fp=fopen("c:\\temp","rt");
    if(fp==NULL)
     return;CString str;
    int index=0;while(fscanf(fp,"%s",str.GetBuffer(256))==1)
    {
    str.ReleaseBuffer();
    s[index]=str;
    index++;
    }fclose(fp);
      

  7.   

    我以前写的:
    CString s[100];
    int i=0,k=0;
    char ch1;
    .
    .
    .
    do
    {
    do
    {
    k=file1.Read(ch1,1);
    if (k==0)
    break;
    else
    {
    if (ch==0x0d)
    file.Read(ch1,1);
    else
    s[i]+=ch1;
    }
    }while(ch!=0x0a);
    i++;
    }while(k!=0);还是Pipi0714的方法简单好用!
      

  8.   


    5F4CCB14  61 62 63 64 65 66 67 63 6F 72 65 2E 63 70 70 00 61 66 78 74 65 6D 70 6C 2E 68 00  abcdefgcore.cpp.afxtempl.h.
    我明明读的61 62 63 64 65 66 67  ,为什么str的值却为"abcdefgcore.cpp"
      

  9.   

    0x61 = a
    .
    .
    .
    0x67 = g
    0x63 = c
    0x6f = o
    0x72 = r
    0x65 = e
    0x2E = .
    0x63 = c
    0x70 = p
    0x70 = p
      

  10.   

    是呀,可我读的是:
    file.Read(str.GetBuffer(str.GetLength()),7);呀
    0x61 = a
    .
    .
    .
    0x67 = g
    都是变红的!!!!!
      

  11.   

    帮我debug一下,看你那里是什么状况!