24小时热门版块排行榜    

查看: 1978  |  回复: 20
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

mljphy

铁虫 (正式写手)

[求助] 一下的数据如何通过C++读取

数据文件例子:test.txt
Frame
3.0   12.0  20.0
1.0    7.0  7.0
15.0  21.0  4.0
Frame
2.0   2.0    15.0
1.0   12.0   3.0
11.0  2.0   23.0
Frame
2.0   21.0   15.0
11.0  12.0   13.0
11.0   2.0   23.0

例子中共有k(k=3)组数据,每组数据n行(n=3),m列(n=3)。

想把它读入a1[k][n],a2[k][n],a3[k][n]数组中,用二维数组保存,要求数组第一个下标对应的是组数k,而第二个下标对应的是行数n,数组名已经区分了列数(实际中代表不同类型的数据,所以用不同数组明区分,后面好进行处理),这里是3列所有用了三个数组a1,a2,a3。
多谢高手指点。
回复此楼

» 猜你喜欢

一切都会慢慢好起来。
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zeppe

金虫 (小有名气)

【答案】应助回帖

★ ★ ★ ★ ★ ★ ★ ★ ★ ★
mljphy: 回帖置顶 2013-06-04 15:40:50
mljphy: 金币+10, ★★★★★最佳答案, zeppe编程水平令人羡慕。以后多想你学习。 2013-06-04 15:42:17
楼主你好,请试用下面这些可以来实现读取17楼提供的文件,假设存为"test_new.txt"
在我电脑上是"D:\\Skydrive\\MyCode\\cpp\\TreatString\\TreatString\\src\\test_new.txt"
请根据你自己的情况修改文件路径和文件名。
另外,如果你方便公开文件的话,最好是直接在附件或网盘给出你的文件,这样不用老改。
CODE:
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <cassert>

int main(void)
{
        // path of the input file ###### please edit this line according to your real path
        std::string filename = "D:\\Skydrive\\MyCode\\cpp\\TreatString\\TreatString\\src\\test_new.txt";
        std::ifstream infile(filename);

        // check if file exists
        if(!infile) {
                std::cerr << "file "<< filename << " not found." << std::endl;
        }

        // variables for the timesteps
        std::vector<double> timesteps;
        bool bTimeStep = false;
        int nTimeStep = 0;

        // variables for the atoms
        std::vector<int> atoms;  
        bool bAtoms = false;
        int nAtoms = 0;

        // variables for the coordinates
        std::vector<double> coordinates;
        bool bCoordinates = false;
        int nCoordinates = 0;

        // variables for the frames
        std::vector<double> frames;   
        bool bFrame = false;
        int nFrame = 0;

        std::string line;  // string that stores each line read in
        int nLine = 0;

        // read file line by line
        while(std::getline(infile, line))
        {
                nLine += 1;
                // read in TimeStep
                if(line == "ITEM: TIMESTEP"){
                        bTimeStep = true;
                        nTimeStep = nLine;
                }
                if(nLine > nTimeStep && bTimeStep){
                        if(line == "Atoms"){
                                bTimeStep = false;
                        }
                        else {
                                double val = std::numeric_limits<double>::quiet_NaN();
                                std::istringstream iss(line);
                                iss >> val;
                                timesteps.push_back(val);
                        }
                }
                // read in Atoms
                if(line == "Atoms"){
                        bAtoms = true;
                        nAtoms = nLine;
                }
                if(nLine > nAtoms && bAtoms){
                        if(line == "Coordinates"){
                                bAtoms = false;
                        }
                        else {
                                int val = 0;
                                std::istringstream iss(line);
                                iss >> val;
                                atoms.push_back(val);
                        }
                }
                //read in coordinates
                if(line == "Coordinates"){
                        bCoordinates = true;
                        nCoordinates = nLine;
                }
                if(nLine > nCoordinates && bCoordinates){
                        if(line == "Frame"){
                                bCoordinates = false;
                        }
                        else {
                                double val1, val2;
                                std::istringstream iss(line);
                                iss >> val1 >> val2;
                                coordinates.push_back(val1);
                                coordinates.push_back(val2);
                        }
                }
                //read in frame
                if(line == "Frame"){
                        bFrame = true;
                        nFrame = nLine;
                }
                if(nLine > nFrame && bFrame){
                        // ############ should be modified when file changes
                        if(line == "......共171行......"){
                                bFrame = false;
                        }
                        else if(line == "ITEM: TIMESTEP") {
                                bFrame = false;
                        }
                        else {
                                double val1, val2, val3, val4, val5;
                                std::istringstream iss(line);
                                iss >> val1 >> val2 >> val3 >> val4 >> val5;
                                frames.push_back(val1);
                                frames.push_back(val2);
                                frames.push_back(val3);
                                frames.push_back(val4);
                                frames.push_back(val5);
                        }
                }
        }

        // check if successfully read
        if(timesteps.empty()){
                std::cerr << "ERROR: failed to read timesteps." << std::endl;
        }
        if(atoms.empty()){
                std::cerr << "ERROR: failed to read atoms." << std::endl;
        }
        if(coordinates.empty()){
                std::cerr << "ERROR: failed to read coordinates." << std::endl;
        }
        if(frames.empty()){
                std::cerr << "ERROR: failed to read frames." << std::endl;
        }
        // ############ should be modified when file changes
        assert(timesteps.size() == atoms.size());
        assert(timesteps.size()*6 == coordinates.size());
        assert(timesteps.size()*5*4 == frames.size());

        //output values
        for(size_t i = 0; i < timesteps.size(); ++i)
        {
                std::cout << "TimeSteps" << std::endl;
                std::cout << timesteps[i] << std::endl;

                std::cout << "Atoms" << std::endl;
                std::cout << atoms[i] << std::endl;

                std::cout << "Coordinates" << std::endl;
                for(size_t j = 0; j < 3; ++j)
                {
                        for(size_t k = 0; k < 2; ++k)
                        {
                                std::cout<<coordinates[6*i + j*2 + k] << "\t";
                        }
                        std::cout << std::endl;
                }

                std::cout << "Frames" << std::endl;
                // ############# should be modified when file changes
                for(size_t m = 0; m < 4; ++m)
                {
                        for(size_t n = 0; n < 5; ++n)
                        {
                                std::cout << frames[20*i + m*5 + n] << "\t";
                        }
                        std::cout<< std::endl;
                }
        }

        return 0;
}

» 本帖已获得的红花(最新10朵)

18楼2013-06-04 11:04:19
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 21 个回答

zhanglinfeng

新虫 (小有名气)

【答案】应助回帖

感谢参与,应助指数 +1
大致的考虑了下 , 你可以用文件流来读取数据,定义三个数组用来存储数据,至于代码要等我测试了在给你!
2楼2013-06-02 18:54:02
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zhanglinfeng

新虫 (小有名气)

【答案】应助回帖


xzhdty: 金币+1, 谢谢参与 2013-06-02 20:49:07
#include<iostream>
#include<fstream>
using std::cout ;

int main()
{
  const char *filename  = "test.txt 所在的绝对路径" ;
  std::ifstream is(filename ) ;
  double a1[3][3] = {0} ;
  double a2[3][3] = {0} ;
  double a3[3][3]  = {0} ;
    for (int k = 0 ; k != 3 ; ++k)
     for (int n = 0 ; n != 3 ; ++n)
   {
      //is >> data ;
       switch(n)
       {
          case 0 :
              is >> a1[k][n] >> a2[k][n] >> a3[k][n];
               break ;
         case 1 :
              is >> a1[k][n] >> a2[k][n] >> a3[k][n]  ;
               break ;
        case 2 :
               is >> a1[k][n] >> a2[k][n] >> a3[k][n]  ;
                 break ;
        }
     }
for (int k = 0 ; k != 3 ; ++k)  //为了测试正确性
35     for (int n = 0 ; n != 3 ; ++n)
36     {
37        cout << a1[k][n] << "\t" << a2[k][n] << "\t" << a3[k][n] << std::endl     ;
38     }

return 0 ;
}
3楼2013-06-02 20:25:07
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

mljphy

铁虫 (正式写手)

引用回帖:
3楼: Originally posted by zhanglinfeng at 2013-06-02 20:25:07
#include<iostream>
#include<fstream>
using std::cout ;

int main()
{
  const char *filename  = "test.txt 所在的绝对路径" ;
  std::ifstream is(filename ) ;
  double a1 =  ...

首先谢谢你的热心帮助。
我测试了一下代码,似乎不对,好像没有读入,所有数据都还是零。
一切都会慢慢好起来。
4楼2013-06-02 21:14:37
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
信息提示
请填处理意见