各位大哥,你们好!
我呢,现在有一个文本文件,里边放的是实型数据,而且呢,在两个实型数局之间还有空格,我呢,现在怎样能把每个实型数局读出放在我的定义好的数组中!我用下面的代码,不行!我应该怎样呢??
float a[2000];
CFile myfile;
myfile.open("文本文件的路径",CFile::modeRead);
myfile.Read(a,myfile.Getlen());
上边的代码就是不行,这是怎么回事??
大哥们,我的文本格式如下:文件名dat.txt
0.000000  4.882812  0.000000            12.207030    
2.441406  4.882812  -7.324218  12.207030    
0.000000  4.882812  -7.324218  12.207030  
2.441406  7.324218  -9.765624  12.207030  
0.000000  4.882812  -9.765624  12.207030  
2.441406  7.324218  -9.765624  12.207030  
0.000000  7.324218  -9.765624  12.207030  
0.000000  4.882812  -7.324218  12.207030  
2.441406  4.882812  -7.324218  14.648436  
0.000000  4.882812  -7.324218  12.207030  
2.441406  7.324218  -7.324218  12.207030  
2.441406  4.882812  -7.324218  12.207030  
2.441406  7.324218  -9.765624  12.207030  
2.441406  7.324218  -7.324218  12.207030  
2.441406  7.324218  -7.324218  12.207030  
0.000000  7.324218  -7.324218  12.207030  
2.441406  4.882812  -7.324218  12.207030  
0.000000  7.324218  -7.324218  14.648436  
2.441406  7.324218  -7.324218  9.765624  
2.441406  7.324218  -9.765624  12.207030  
0.000000  4.882812  -7.324218  12.207030  
0.000000  4.882812  -7.324218  12.207030  
2.441406  7.324218  -7.324218  12.207030

解决方案 »

  1.   

    no!!
    i try ,but it doesnot work!!
      

  2.   

    先看看你的文件打开了没有?还有,只能读入字符串中,不能用float
      

  3.   

    试试
    float a[2000];
    CFile myfile;
    myfile.open("文本文件的路径",CFile::modeRead|CFile::typeText);
    myfile.Read(a,myfile.Getlen());或
    float a[2000];
    FILE *myfile;
    myfile=fopen("文本文件的路径","rt");
    fscanf(myfile,"%f",a[i]);行不行?
      

  4.   

    CString strFile;
    CFile myfile;
    myfile.open("文本文件的路径",CFile::modeRead);
    iLen=myfile.Read(strFile.GetBuffer(myfile.GetLen),myfile.Getlen());
    strFile.ReleaseBuffer();
    for(iPos=0;iPos<iLen;iPos+=10)
    {
      atof(strFile.Mid(iPos,8))
    }
    myFile.close();
    你的文件格式我不太清楚,所以for循环你自己调整一下
      

  5.   

    谁说用fscnaf不行? 下面代代码我刚刚运行过,根据你给的文件格式,一切OK!  FILE* fp=fopen("F:\\Inprise\\Software\\Hpmqy\\data.txt","r");
      double data[4];
      char c;  while(!feof(fp))
      {
        fscanf(fp,"%lf%lf%lf%lf\n",&data[0],&data[1],&data[2],&data[3],&c);
        //...
      }
      

  6.   

    CStdioFile fp;
    CString store;
    try
    {
    fp.ReadString(store);
    }
    catch(CFileException *file)
    {
     file->ReportError();
    }
      

  7.   

    void read_dat(const char* filename)
    {
    ifstream in(filename);
    float a[10000]; int i = 0;
    while(!in.eof()){
    in >> a[i++];
    } int n = i;
    for(i = 0; i < n; i++){
    cout << a[i] << "\t";
    if(i % 4 == 0 && i != 0) cout << endl;
    }
    }
      

  8.   

    先用GETLINE读入一行,然后读入到流数组中
    char bb[255];
    ifstream f("filename");
    char buff[255]=f.getline();
    ostrstream buffer(bb,ios::in|ios::out);
    buffer<<buff;
    buffer>>value1>>value2>>value3;    value存放每一列数据,类型自己定义。
    这样的话就以可以了,你读的数据就是value1=0.000000
                                   value2=4.882812
                                   value3=0.000000
                                   value4=12.207030
    处理这些数据并循环GETLINE。
      

  9.   

    先用GETLINE读入一行,然后读入到流数组中
    char bb[255];
    ifstream f("filename");
    char buff[255]=f.getline();
    ostrstream buffer(bb,ios::in|ios::out);
    buffer<<buff;
    buffer>>value1>>value2>>value3;    value存放每一列数据,类型自己定义。
    这样的话就以可以了,你读的数据就是value1=0.000000
                                   value2=4.882812
                                   value3=0.000000
                                   value4=12.207030
    处理这些数据并循环GETLINE。