将数据从文件分配给矩阵C++

Assigning data from file to matrix C++

本文关键字:C++ 文件分配 数据      更新时间:2023-10-16

大家好,我是C++初学者。我想要达到的目标是安静愚蠢的。但我看不到/明白错误在哪里。我将非常感谢任何蓝码帮助。

所以。。。我想分配文件"REL"的内容,这是一个 46x2 [无空行]的矩阵:

28 28
28  6
28 21
28 30
28 16
22 22
22 33
22 9
39 39
39 32
39 46
39 10
39 24
36 36
36 7
36 43
36 23
11 11
11 26
11 41
15 15
15 17
15 45
15 29
15 40
3 3
3 37
3 42
3 34
3 2
35 35
35 4
35 14
35 44
35 18
13 13
13 12
13 25
13 5
13 1
13 8
13 31
20 20
20 27
20 19
20 38

我的C++代码是:

    include <stdlib.h>
    include <malloc.h>
    include <iostream>
    include <fstream>
    using namespace std;
    void allocateMatrix(int **& A, int row, int col)
    {
        int i;
        A = new int*[row];
        for (i=1; i<=col; i++)
            A[i] = new int[col];
    }
    void ReadData(int **& A, int row, int col) // read data from file
    {  
    int i,j;
    ifstream REL1;
    REL1.open ("REL");  // open file for reading                
    for(i=1;i<=row;i++)   // row loop
    {
        for(j=1;j<=col;j++)  // column loop
        {
            REL1 >> A[i][j]; // read data into matrix
        }
        REL1.close(); // close the file                
    }
    }
    void Display(int **& A, int row, int col) // display matrix
    { 
    int i,j;
    for(i=1;i<=row;i++)
    {
        for(j=1;j<=col;j++)
        { 
            cout << A[i][j] << "t ";  // display numbers
        }
        cout << endl;
    }    
    }
    void Cero(int **& A, int row, int col) // display matrix
    {
    int i,j;
    for(i=1;i<=row;i++)
    {
        for(j=1;j<=col;j++)
        {
            A[i][j]=0;
        }
    }   
    }   
    int main()
    {
    int tm,rowR,colR,rowM,colM,**A,**B; 
    ifstream TM;
    TM.open("TM");
    TM >> tm;
    TM.close();
    rowR = rowM = colM = tm;
    colR = 2;
    allocateMatrix(A, rowM, colM);
    allocateMatrix(B, rowR, colR);
    Cero(A, rowM, colM); 
    //Display(A, rowM, colM);
    ReadData(B ,rowR, colR);
    Display(B, rowR, colR);
    }

而且......当我在 bash 中运行它时,shell 提示我:

28   28  
0    0   
Segmentation fault (core dumped)

提前谢谢!!

for(i=1;i<=row;i++)   // row loop
{
    for(j=1;j<=col;j++)  // column loop
    {
        REL1 >> A[i][j]; // read data into matrix
    }
    REL1.close(); // close the file                
}

读取第一行后关闭文件。将文件关闭行移出循环。

for(i=1;i<=row;i++)   // row loop
{
    for(j=1;j<=col;j++)  // column loop
    {
        REL1 >> A[i][j]; // read data into matrix
    }               
}
REL1.close(); // close the file 

数组索引从 0 开始,而不是 1。

下面是一个典型的、健壮的,但不是完全最佳的读取输入的通用方法:首先读取每一行,然后将每一行解析为标记。当然,可以添加长度检查,这样您就不会尝试读取数十亿行的文件,但我将其留给您。

请注意,我们从不显式调用close(),因为这不是必需的。

#include <iostream>
#include <sstream>
#include <iterator>  // for istream_iterator
#include <string>    // for getline and string
#include <vector>    // for vector
#include <cstdlib>   // for exit
namespace
{
    void die(int code, char const * msg)
    {
        std::cerr << msg << std::endl;
        std::exit(code);
    }
}
int main()
{
    std::ifstream tm("TM.txt");
    if (!tm) { die(1, "Could not open file."); }
    std::vector<std::vector<int>> matrix;
    for (std::string line; std::getline(tm, line); )
    {
        std::istringstream iss(line);
        std::vector<int> row(2);
        if (!(iss >> row[0] >> row[1])) { die(1, "Invalid line!"); }
        matrix.push_back(row);
    }
    // done, now use matrix[i][j]
}

在 C++11 中,您可以添加各种小的优化,但现在不要介意。在更一般的设置中,您还可以删除每行仅包含两个元素的限制,而是读取尽可能多的元素,可能会检查最终大小:

std::vector<int> row(std::istream_iterator<int>(iss), std::istream_iterator<int>());
// check that row.size() has the desired value!

最后,您可能希望向外部循环添加一个计数器,如果行数超过预期(例如,在本例中为 46 行(则中止,这样您就不会被巨大的输入文件破坏。

感谢您@KerrekSB回复。这种级别的C++编程比C++的基本知识领先一步。尽管如此,我还是尝试了您的建议并按照发布的方式重写了代码:

#include <stdlib.h>
#include <malloc.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iterator>  // for istream_iterator
#include <string>    // for getline and string
#include <vector>    // for vector
#include <cstdlib>   // for exit
using namespace std;
namespace
{
    void die(int code, char const * msg)
    {
        std::cerr << msg << std::endl;
    std::exit(code);
    }
}
void AllocateMatrix(int **& A, int row, int col)
{
int i;
A = new int*[row];
for (i=0; i<col; i++)
    A[i] = new int[col];
}
void ReadData(int **& A, int row, int col) // read data from file
{  
int i,j,in;
ifstream REL1;
REL1.open ("REL");  // open file for reading                
for(i=0;i<row;i++)   // row loop
{
    REL1 >> in; // read data into matrix
    for(j=0;j<col;j++)  // column loop
    {
        A[i][j]=in;
    }
}
REL1.close(); // close the file                
}
void Display(int **& A, int row, int col) // display matrix
{ 
int i,j;
for(i=0;i<row;i++)
{
    for(j=0;j<col;j++)
    { 
        cout << A[i][j] << "t ";  // display numbers
    }
}
cout <<endl;    
}
void Cero(int **& A, int row, int col) // display matrix
{
int i,j;
for(i=0;i<row;i++)
{
    for(j=0;j<col;j++)
    {
        A[i][j]=0;
    }
}   
}   
int main()
{
int tm,rowR,colR,rowM,colM,**A,**B; 
ifstream TM;
TM.open("TM");
TM >> tm;
TM.close();
rowR = 46; 
rowM = colM = tm;
colR = 2;
AllocateMatrix(A, rowM, colM);
AllocateMatrix(B, rowR, colR);
Cero(A, rowM, colM); 
//Display(A, rowM, colM);
//ReadData(B ,rowR, colR);

std::ifstream rel("REL");
if (!rel) { die(1, "Could not open file."); }
std::vector<std::vector<int> > matrix;
for (std::string line; std::getline(rel, line); )
{
    std::istringstream iss(line);
    std::vector<int> row(2);
    if (!(iss >> row[0] >> row[1])) { die(1, "Invalid line!"); }
    matrix.push_back(row);
}


Display(B, rowR, colR);
}

当我执行它时,它会弹出:

Invalid line!