将数据从文件分配给矩阵C++
Assigning data from file to matrix C++
大家好,我是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!
相关文章:
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 使用 Libssh 将文件从服务器复制到客户端:分配文件复制目标路径时出现问题
- 如何通过一个循环将多个文件的内容分配给不同的类属性?
- 如何从文件中读取单词,将它们分配给数组并分析其内容?
- 如何从文件读取到动态分配的指向结构的指针链表中?
- 如何将 QML 文件分配给主窗口/应用程序
- 在读取大型 txt 文件时动态分配结构内的向量
- 字符串和输入文件,从包含字符串的 txt 文件中分配多个变量值
- 如何将文本文件的部分分配给数组的索引
- 动态分配读取 txt 文件的内存
- C++从 csv 文件中读取,然后将数据分配给类成员
- 我想从文本文件中获取信息并将该信息分配给类对象
- 从文件中分配变量,并使用定界符分配
- 如何将足够的数据从文本文件分配到char*中,然后将其复制到内存C 中
- 如何在 Linux 下为可执行文件分配堆栈
- 如何直接从文本文件分配变量
- 为什么不能将易失性文件*分配给文件*?
- 将数据从文件分配给矩阵C++
- 当提升内存映射文件分配的磁盘空间超过HDD上的可用空间时,如何检测错误
- 用c++在追加模式下打开一个文件进行写入会为整个文件分配内存吗