如何从文件中读取值,然后使用它们初始化二维数组 C++
How to read values from a file and then use them to initialize a 2D array C++
我希望读取文本文件,例如
4
2
3
4 3
2 1
5 4
8 4
第一行是二维数组的第一维,第二行是二维数组的第二维,第三行是一个值。在读取前三个值并将其存储到变量 n、m、k 后,我想初始化 2D int 数组 int x[n][m].
#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
using namespace std;
int n,m,k;
int main()
{
ifstream File;
File.open("text.txt");
if (File.fail()) {
cout << "error opening file";
}
while (!File.eof())
{
File >> n;
File >> m;
File >> k;
int x[n][m];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
File >> x[i][j];
}
}
}
return 0;
}
但是,我无法初始化数组,因为表达式 n 和 m 似乎没有常量值。如果我将变量设置为 const int n,m,k,我将无法使用 >> 从文件中读取它们。我如何读取数组大小调整值,使用它们创建数组,然后将值存储在其中?
编辑:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <math.h>
using namespace std;
std::vector < std::vector <int>> x;
int n,m,k;
int main()
{
ifstream File;
File.open("test.txt");
if (File.fail()) {
cout << "error opening file";
}
while (!File.eof())
{
File >> n;
File >> m;
File >> k;
for (int i = 0; i < n; i++) {
vector <int> row;
for (int j = 0; j < m; j++) {
int readFromFile = 0;
File >> readFromFile;
row.push_back(readFromFile);
}
x.push_back(row);
}
}
cout << n;
cout << "\n";
cout << m;
cout << "\n";
cout << k;
cout << "\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << x[i][j];
cout << " ";
}
cout << "\n";
}
return 0;
}
#include <cstdlib> // EXIT_FAILURE
#include <vector>
#include <fstream>
#include <iostream>
int main()
{
char const * file_name{ "test.txt" };
std::ifstream is{ file_name };
if (!is.is_open()) { // exit if the file couldn't be opened.
std::cerr << "Couldn't open \"" << file_name << "\" for reading!\n\n";
return EXIT_FAILURE;
}
int m, n, k;
if (!(is >> m >> n >> k)) { // whatever k might be.
std::cerr << "Data error!\n\n";
return EXIT_FAILURE;
}
std::vector<std::vector<int>> data(m, std::vector<int>(n)); // m rows, n cols
for (int row = 0; row < m; ++row) {
for (int col = 0; col < n; ++col) {
if (!(is >> data[row][col])) { // try to extract an integer
std::cerr << "Data error!\n\n"; // give an error message and
return EXIT_FAILURE; // exit if it fails
}
}
}
for (auto const &row : data) {
for (auto const &col : row) {
std::cout << col << ' ';
}
std::cout.put('\n');
}
}
输出:
4 3
2 1
5 4
8 4
我希望读取文本文件,例如
4
2
3
4 3
2 1
5 4
8 4
第一行是二维数组的第一维,第二行是二维数组的第二维,第三行是一个值。在读取前三个值并将其存储到变量 n、m、k 后,我想初始化 2D int 数组 int x[n][m].
#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
using namespace std;
int n,m,k;
int main()
{
ifstream File;
File.open("text.txt");
if (File.fail()) {
cout << "error opening file";
}
while (!File.eof())
{
File >> n;
File >> m;
File >> k;
int x[n][m];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
File >> x[i][j];
}
}
}
return 0;
}
但是,我无法初始化数组,因为表达式 n 和 m 似乎没有常量值。如果我将变量设置为 const int n,m,k,我将无法使用 >> 从文件中读取它们。我如何读取数组大小调整值,使用它们创建数组,然后将值存储在其中?
编辑:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <math.h>
using namespace std;
std::vector < std::vector <int>> x;
int n,m,k;
int main()
{
ifstream File;
File.open("test.txt");
if (File.fail()) {
cout << "error opening file";
}
while (!File.eof())
{
File >> n;
File >> m;
File >> k;
for (int i = 0; i < n; i++) {
vector <int> row;
for (int j = 0; j < m; j++) {
int readFromFile = 0;
File >> readFromFile;
row.push_back(readFromFile);
}
x.push_back(row);
}
}
cout << n;
cout << "\n";
cout << m;
cout << "\n";
cout << k;
cout << "\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << x[i][j];
cout << " ";
}
cout << "\n";
}
return 0;
}
#include <cstdlib> // EXIT_FAILURE
#include <vector>
#include <fstream>
#include <iostream>
int main()
{
char const * file_name{ "test.txt" };
std::ifstream is{ file_name };
if (!is.is_open()) { // exit if the file couldn't be opened.
std::cerr << "Couldn't open \"" << file_name << "\" for reading!\n\n";
return EXIT_FAILURE;
}
int m, n, k;
if (!(is >> m >> n >> k)) { // whatever k might be.
std::cerr << "Data error!\n\n";
return EXIT_FAILURE;
}
std::vector<std::vector<int>> data(m, std::vector<int>(n)); // m rows, n cols
for (int row = 0; row < m; ++row) {
for (int col = 0; col < n; ++col) {
if (!(is >> data[row][col])) { // try to extract an integer
std::cerr << "Data error!\n\n"; // give an error message and
return EXIT_FAILURE; // exit if it fails
}
}
}
for (auto const &row : data) {
for (auto const &col : row) {
std::cout << col << ' ';
}
std::cout.put('\n');
}
}
输出:
4 3
2 1
5 4
8 4