zoukankan      html  css  js  c++  java
  • C++ 从TXT文档中读取一个矩阵

    最近做GPU编程方面的问题,需要将MATLAB代码改成CUDA,其中就涉及到用C++

    从一般的文件中读取一个矩阵的问题,MATLAB读取矩阵很容易,但在C++中使用矩阵非常麻烦,

    基本都要用地址访问的方式,对于使用C++ 不是很熟练的来说,也许读数据都要大费周章.

    template<typename T>
    void readMatrixfromTXT(const char *fileName, const int numColumn, const int numRow, T *matrix)
    {
    // std::ifstream fin(fileName,std::ifstream::in);
    ifstream fin(fileName, ios::in);
    // ifstream fin(fileName.c_str(),ios::in);
    if (!fin)
    {
    cerr << "不能打开文件" << endl;
    exit(1);
    }
    string line;
    float tmp;
    int j = 0;
    for (int i = 0; i<numRow; i++){
    getline(fin, line);
    j = 0;
    //for(int j=0;j<numColumn;j++){
    istringstream istr(line);
    while (istr >> tmp){
    matrix[i*numColumn + j] = tmp;
    ++j;
    //cout<<tmp<<endl;
    }
    istr.clear();
    line.clear();
    }
    fin.close();
    }

  • 相关阅读:
    spring整合myBatis
    spring之事物
    spring之AspectJ实现AOP
    AOP之JDK动态代理和CGLib动态代理
    iOS-面试相关<一>
    iOS -调试工具Instruments使用总结
    iOS-阅读器常年崩溃问题记录
    iOS
    ios
    iOS
  • 原文地址:https://www.cnblogs.com/Erdos001/p/4496221.html
Copyright © 2011-2022 走看看