各位大侠,我是新手,请大家多多关照。    我想请教一个问题有如下的TXT文件:!Agilent Technologies,N5230A,US43500367,A.07.50.37
!Agilent N5230A: A.07.50.37
!Date: Friday, November 27, 2009 10:22:43
!Correction: S11(Full 1 Port(1)) 
!S1P File: Measurement: S11:
# Hz S  RI   R 50
10000000  -9.967119e-001  3.493446e-003 
39950000  -9.925810e-001  1.823045e-002 
69900000  -9.919273e-001  2.857167e-002 
99850000  -9.908134e-001  4.461637e-002 
129800000  -9.883738e-001  5.682826e-002 
159750000  -9.898794e-001  6.714757e-002 
189700000  -9.879245e-001  8.252413e-002 
219650000  -9.820263e-001  9.321935e-002 
249600000  -9.809552e-001  1.026641e-001 
(下面省略若干行数据) 
    我在MFC程序中打开该TXT文件,想从第7行开始,把接下来的的数据存入一个二维数组,请问怎么实现?
 
    我想通过CStdioFile流来实现,但是网上查了好久资料,还是没有头绪。    请各位不吝赐教。  不胜感激~~~

解决方案 »

  1.   

    很简单的问题,查下MSDN的函数说明。void CMFCTestDlg::OnBnClickedOk()
    {
    // TODO: 在此添加控件通知处理程序代码
    CString str;
    CStdioFile fp(TEXT("D:\\1.txt"), CFile::modeRead | CFile::typeText);
    if (fp.m_pStream)
    {
    for (int i=0; i<6; i++)
    {
    fp.ReadString(str);
    }//跳过前6行
    while(fp.ReadString(str))
    {
    MessageBox(str);
    }
    fp.Close();
    }
    OnOK();
    }
      

  2.   

       应该可行....
       自己把数据存入数组的那段补上看看。    我把我自己的代码贴出来:CStdioFile file;
                    file.Open("data.txt",CFile::typeText|CFile::modeRead); int line=1;
    CString str;
    while( (line++<7) && file.ReadString(str))
    {
    //MessageBox(NULL,str,"",MB_OK);
    } const int ROWS=1000;
    double arr[ROWS][3]={0};
    int i=0,j=0; //数组下标
    double num=0; //从字符串中解析出来的数值放在num中
    while(file.ReadString(str))
    {
    // MessageBox(NULL,str,"",MB_OK);
    int beginPos=0;
    int pos=0;
    string line(str.GetBuffer(str.GetLength()));//类型转化,以使用find_first_of函数
    string temp;
    int times=0;
    while(times++<3)
    {
    pos=line.find_first_of("  ",beginPos);
    temp=line.substr(beginPos,pos-beginPos);
    beginPos=pos+2;
    num=atof(temp.c_str()); //字符串转化为double
    arr[i][j++]=num;

    // CString tt;
    // tt.Format("%f", num);
    // MessageBox(NULL,tt,"",MB_OK);
    }
    i++;
    j=0;
    }
    file.Close();