C++ 在运行时确定行数和列数时,将数字表读入数组

C++ 在运行时确定行数和列数时,将数字表读入数组,c++,arrays,getline,C++,Arrays,Getline,我想问你有关数据输入的问题。我有一个以下格式的文本文件: 7.2 xy 10 2 0.048922 3 0.0978829 4 0.146908 5 0.196019 6 0.245239 7 0.294584 第一行包含要读入的行数和列数。第二行是标题。从第三行开始,它只是数据。我想将我的数据读入一个2D数组(mat[]),并将标题读入一个字符串数组(title[]),以后可以很容易地引用。我写剧本已经走了这么远。它可以将数组编号读入数组,但不能读入二维数组。我试图为传递给函数的矩阵声明一个指

我想问你有关数据输入的问题。我有一个以下格式的文本文件:

7.2

xy

10
2 0.048922
3 0.0978829
4 0.146908
5 0.196019
6 0.245239
7 0.294584

第一行包含要读入的行数和列数。第二行是标题。从第三行开始,它只是数据。我想将我的数据读入一个2D数组(mat[]),并将标题读入一个字符串数组(title[]),以后可以很容易地引用。我写剧本已经走了这么远。它可以将数组编号读入数组,但不能读入二维数组。我试图为传递给函数的矩阵声明一个指针,但我做不到。我还尝试了getline(),但不知道如何将分隔为不同字符串的标题分隔开

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

void readFile(char[]);

int main(){

    char fileName[] = "results.txt";

    readFile(fileName);
    return 0;
}


// Read file to matrix
void readFile(char fileName[]){

    int m, n;

    // Create streamobject
    ifstream infile;
    infile.open(fileName);

    // Exit if file opening failed
    if (!infile.is_open()){
        cerr<<"Opening failed"<<endl;
        exit(1);
    }

    // Get size of the matrix
    infile >> m >> n;

    // Pre-allocate memory
    string title;
    float *mat=new float[m*n];

    // Read title

    // Start reading data
    while (!infile.eof()){
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                infile >> mat[i*n+j];
            }
        }
    }

    infile.close();
}
#包括
#包括
#包括
#包括
使用名称空间std;
void readFile(char[]);
int main(){
char fileName[]=“results.txt”;
读取文件(文件名);
返回0;
}
//将文件读取到矩阵
无效读取文件(字符文件名[]){
int m,n;
//创建streamobject
河流充填;
infile.open(文件名);
//如果文件打开失败,请退出
如果(!infle.is_open()){
欧洲核子研究中心;
//预分配内存
字符串标题;
浮球*mat=新浮球[m*n];
//阅读标题
//开始读取数据
而(!infle.eof()){
对于(int i=0;i mat[i*n+j];
}
}
}
infle.close();
}
有人能帮我吗


感谢您到目前为止的帮助。我将在本周末看一看应用向量,但我必须仔细阅读。我已经快速更改了脚本,现在它可以执行我想要的操作:返回一个指向数组“mat”的指针和值。我有一个名为“showMatrix”的函数,它可以将其打印到屏幕上。 如果我在函数“readFile”中调用showMatrix,它可以正常工作。另一方面,如果我在主脚本外部调用它,那么它将返回完全错误的值

我感觉这里有点不对劲。你能帮我指出一下吗

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

void readFile(char[],float *mat,int &m,int &n);
void showMatrix(float *mat,int m,int n);


/*******************************************************************
 Script
 *******************************************************************/
int main(){

    char fileName[] = "results.txt";
    int m, n;
    float *mat;

    // Read data from file
    readFile(fileName,mat,m,n);
    showMatrix(mat,m,n);

    return 0;
}

// Read file to matrix
void readFile(char fileName[],float *mat,int &m,int &n){


    // Create streamobject
    ifstream infile;
    infile.open(fileName);

    // Exit if file opening failed
    if (!infile.is_open()){
        cerr<<"Opening failed"<<endl;
        exit(1);
    }

    // Get size of the matrix
    infile >> m >> n;

    // Pre-allocate memory
    mat=new float[m*n];

    // Read title
    std::string X;
    std::string Y;
    infile >> X >> Y;

    // Reading data
    while (!infile.eof()){
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                infile >> mat[i*n+j];
            }
        }
    }
//    showMatrix(mat,m,n);
    infile.close();
}

// Print matrix to screen
void showMatrix(float *x,int m, int n){
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){
            cout << x[i*n+j] << "\t";
        }
        cout << endl;
    }
}
#包括
#包括
#包括
#包括
使用名称空间std;
无效读取文件(char[],float*mat,int&m,int&n);
void showMatrix(float*mat,int m,int n);
/*******************************************************************
剧本
*******************************************************************/
int main(){
char fileName[]=“results.txt”;
int m,n;
浮垫;
//从文件中读取数据
readFile(文件名,mat,m,n);
显示矩阵(mat,m,n);
返回0;
}
//将文件读取到矩阵
void readFile(char fileName[],float*mat,int&m,int&n){
//创建streamobject
河流充填;
infile.open(文件名);
//如果文件打开失败,请退出
如果(!infle.is_open()){
欧洲核子研究中心;
//预分配内存
mat=新浮点数[m*n];
//阅读标题
std::字符串X;
std::字符串Y;
填充>>X>>Y;
//读取数据
而(!infle.eof()){
对于(int i=0;i mat[i*n+j];
}
}
}
//显示矩阵(mat,m,n);
infle.close();
}
//将矩阵打印到屏幕
void showMatrix(浮点*x,整数m,整数n){

对于(int i=0;i要在函数中分配二维数组,请使用:

float **mat = new float*[m];

for(int i=0; i<m; i++){
    mat[i] = new float[n];
}
最后,确保您实现了阅读标题;现在它是空的:

//Read title

要在函数中分配二维数组,请使用:

float **mat = new float*[m];

for(int i=0; i<m; i++){
    mat[i] = new float[n];
}
最后,确保您实现了阅读标题;现在它是空的:

//Read title
<>在C和C++中,数组的指针(C和C++)和引用(C++)仅用一种有点模糊的语法来声明,例如:

void fp(float (*array)[10]);
void fr(float (&array)[10]);
void f2d(float (&array)[10][10]);


void main()
{
    float a[10];
    fp(&a);
    fr(a);

    float b[10][10];
    f2d(b);
}
void f(float* data, int length);
无法声明指向非编译时常量大小的数组的指针或引用。若要传递可变大小的数据,需要使用指向数组中第一个元素的指针并传入数据大小,这样代码就可以安全地使用数组而不会超出范围,如下所示:

void fp(float (*array)[10]);
void fr(float (&array)[10]);
void f2d(float (&array)[10][10]);


void main()
{
    float a[10];
    fp(&a);
    fr(a);

    float b[10][10];
    f2d(b);
}
void f(float* data, int length);
<>但是,这会给程序员带来一个负担,要仔细编写安全正确的代码。因为你使用C++而不是C,所以使用<代码>:ST::vector < /COD>高度推荐!<代码> vector < /C>是一个顺序容器对象,当你需要索引时,它就像一个数组。它是动态可调整的,并且将处理DE。在C和C++中,分配和分配的尾部。< /P> < P>,指针(C和C++)和数组的引用(C++)仅用一种有点模糊的语法来声明,例如:

void fp(float (*array)[10]);
void fr(float (&array)[10]);
void f2d(float (&array)[10][10]);


void main()
{
    float a[10];
    fp(&a);
    fr(a);

    float b[10][10];
    f2d(b);
}
void f(float* data, int length);
无法声明指向非编译时常量大小的数组的指针或引用。若要传递可变大小的数据,需要使用指向数组中第一个元素的指针并传入数据大小,这样代码就可以安全地使用数组而不会超出范围,如下所示:

void fp(float (*array)[10]);
void fr(float (&array)[10]);
void f2d(float (&array)[10][10]);


void main()
{
    float a[10];
    fp(&a);
    fr(a);

    float b[10][10];
    f2d(b);
}
void f(float* data, int length);
<>但是,这会给程序员带来一个负担,要仔细编写安全正确的代码。因为你使用C++而不是C,所以使用<代码>:ST::vector < /COD>高度推荐!<代码> vector < /C>是一个顺序容器对象,当你需要索引时,它就像一个数组。它是动态可调整的,并且将处理DE。分配和解除分配的详细信息。

注意:

while (!infile.eof()){
几乎总是错误的。如果文件读取由于eof以外的任何其他原因失败,那么您将进入一个无限循环

此外,代码的组织方式也意味着您尝试并初始化数据两次(尽管在第二次传递时会失败,并且不会执行任何操作(除了重新执行循环))由于只有读取超过eof时才会设置eof标志,并且由于您知道要读取的第一次传递的数据量不会超过eof,因此您将一直读取到(但不会超过)

读入一维数组

int main()
{
    std::ifstream file("plop");

    int n;
    int m;
    std::string ColHeadX;
    std::string ColHeadY;
    // You forgot to read the Col Header out.
    // Just read them into strings.
    file >> n >> m >> ColHeadX >> ColHeadY;

    // Don't do manual memory management.
    // Use a vector it will do the work for you.
    std::vector<double>   data(n*m); // initialized with (n*m) elemensts

    // Always put the read in the loop test
    //      This bit:  file >> data[loop]
    // If this read fails you want the loop to exit immediately.
    // By putting the read here the result of the read will be
    // tested to see if it worked.
    for(int loop=0;loop < (n*m) && (file >> data[loop]); ++loop) { /*Empty*/}
}
intmain()
{
std::ifstream文件(“plop”);
int n;
int m;
std::字符串ColHeadX;
性病:弦冷淡;
//您忘了读出列标题。
//绝对法