首页 > 解决方案 > 从文件中读取数组的最佳方法

问题描述

我在一个文件中写了一个数组,现在我试图从同一个文件中读取它。但是,当我打印它时,它会给出非常奇怪的数字。我想知道这些数字是从哪里来的(是地址吗?),我该如何解决。我想知道是否可以写一些东西,这样你就不必每次使用 is 时都写相同的循环(我指的是带有 i 和 j 的那个)。

#include <iostream> 
#include <iomanip>
#include <fstream>
#include <time.h> 

int main () {
 int n,m;
 
 cout<<"Enter number of rows: "<<endl;
 cin>>n; 
 cout<<"Enter number of columns: "<<endl;
 cin>>m;
 
 int** b = new int*[n];
 for (int i = 0; i < n; ++i)
        { b[i] = new int[m]; }
    
 srand(time(0));
 
 int max=9, min=-9;
 for( int i = 0; i<n; ++i) {
     for( int j = 0; j<m; ++j) {
         b[i][j] = (rand()%
                     (max-min+1))+min;}
  }
  
 
 cout << " Array :" << endl;
 for (int i=0; i<n; i++) {
 for (int j=0; j<m; j++) {
 cout << setw(5) << b[i][j];
 }
 cout<< endl;
 }
 
 ofstream array ("Array_2D.txt", ios::out);
 for(int i=0; i<n; i++){
     for (int j=0; j<m; j++) {
          array << b[i][j] ;
     }
 }

        ifstream readarray ("Array_2D.txt" , ios :: in);
    
     int** p = new int*[n];
     for (int i = 0; i < n; ++i)
            { p[i] = new int[m]; }
    
     for (int i=0; i<n; ++i)
     for (int j=0; j<m; ++j)
     readarray >> p[i][j]; 
     
    cout << " Array C: " << endl;
     for(int i=0; i<n; ++i ) { // Вывод матрицы на экран
     for(int j=0; j<m; ++j)
     cout << p[i][j] << " ";
     cout << endl;
     }

标签: c++arraysfilepointers

解决方案


试试这个:

#include <iostream> 
#include <iomanip>
#include <fstream>
#include <time.h> 
using namespace std;

int main() {
    int n, m;

    cout << "Enter number of rows: " << endl;
    cin >> n;
    cout << "Enter number of columns: " << endl;
    cin >> m;

    int** b = new int* [n];
    for (int i = 0; i < n; ++i)
    {
        b[i] = new int[m];
    }

    srand(time(0));

    int max = 9, min = -9;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            b[i][j] = (rand() %
                (max - min + 1)) + min;
        }
    }


    cout << " Array :" << endl;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cout << setw(5) << b[i][j];
        }
        cout << endl;
    }

    ofstream array("Array_2D.txt", ios::out);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            array << b[i][j] << " ";
        }
        array << endl;
    }

    ifstream readarray("Array_2D.txt", ios::in);

    int** p = new int* [n];
    for (int i = 0; i < n; ++i)
    {
        p[i] = new int[m];
    }

    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
            readarray >> p[i][j];

    cout << " Array C: " << endl;
    for (int i = 0; i < n; ++i) { // Вывод матрицы на экран
        for (int j = 0; j < m; ++j)
            cout << p[i][j] << " ";
        cout << endl;
    }
}

最初,您没有在数字之间写入空格,因此它只读取一个奇怪的数字,而其他数字未初始化。

示例 I/O:

Enter number of rows:
5
Enter number of columns:
5
 Array :
    9    7    3    7    7
   -6   -2   -1    5   -4
    5   -5    8   -4   -7
   -1    7   -9   -5    3
   -5   -2    1    0   -8
 Array C:
9 7 3 7 7
-6 -2 -1 5 -4
5 -5 8 -4 -7
-1 7 -9 -5 3
-5 -2 1 0 -8

推荐阅读