将文件的元素加载到c++; 我对C++非常陌生(这实际上是我所做过的第一个程序),我写这个程序的时候,我不得不学习它,而且我被卡住了。p>

将文件的元素加载到c++; 我对C++非常陌生(这实际上是我所做过的第一个程序),我写这个程序的时候,我不得不学习它,而且我被卡住了。p>,c++,arrays,file,C++,Arrays,File,程序应该读入一个文本文件,将其加载到一个数组中,并为数组中的每个数字分配一个字符(为此,我想我将创建两个数组,一个从文件中读入数字的int数组,以及一个分配给数字的字符数组)。然后它将打印这两个数组。然后,它应该遍历初始int数组,搜索其值与相邻值相差超过一的任何数字,如果找到这样一个值,它应该为该数字提供其相邻值的平均值。它应该进行所有这些更正,然后为这些更正的数字分配字符,并打印出两个数组 我真的不知道该怎么做,但我会尽量把我的问题缩小到我最初的问题。我不知道如何从文件中加载数组。我的教科书

程序应该读入一个文本文件,将其加载到一个数组中,并为数组中的每个数字分配一个字符(为此,我想我将创建两个数组,一个从文件中读入数字的int数组,以及一个分配给数字的字符数组)。然后它将打印这两个数组。然后,它应该遍历初始int数组,搜索其值与相邻值相差超过一的任何数字,如果找到这样一个值,它应该为该数字提供其相邻值的平均值。它应该进行所有这些更正,然后为这些更正的数字分配字符,并打印出两个数组

我真的不知道该怎么做,但我会尽量把我的问题缩小到我最初的问题。我不知道如何从文件中加载数组。我的教科书似乎很好地涵盖了数组和文件,但它并没有真正将它们结合在一起讨论如何从文件构建数组

下面是文件的外观,第一个数字是数组的大小

10
7 6 9 4 5 4 3 2 1 0
6 5 5 5 6 5 4 3 2 1
6 5 6 6 7 6 8 4 3 2
1 5 6 7 7 7 6 5 4 3
5 5 6 7 6 7 7 6 5 9
5 6 7 6 5 6 6 5 4 3
5 6 7 9 5 5 6 5 4 3
5 5 6 7 6 6 7 6 5 4
5 9 5 6 7 6 5 0 3 2
5 5 5 5 6 5 4 3 2 7
这是我到目前为止所拥有的代码,尽管其中绝大多数是关于打开文件的。我假设它至少是正确的,因为当我运行它时,我没有得到“无法打开文件错误”

#包括
#包括
#包括
int main(){//main将在prog1_测试中
//打开文件
ifstream prog1文件(“prog1.dat”,ios::in);
//如果无法打开文件,请退出
如果(!prog1File){

cerr只要用ifstream替换ofstream,用ios::in替换ios::out即可

#include <iostream>
#include <fstream>
#include <array>

int main() { // Main will be in prog1_test
    //Open file
    ifstream prog1File("prog1.dat", ios::in);

    //If file can't be opened, exit
    if (!prog1File) {
        cerr << "File could not be opened" << end;
        exit(EXIT_FAILURE);
    }
        int size;
        int numArray[][];
 }
#包括
#包括
#包括
int main(){//main将在prog1_测试中
//打开文件
ifstream prog1文件(“prog1.dat”,ios::in);
//如果无法打开文件,请退出
如果(!prog1File){

cerr只要用ifstream替换ofstream,用ios::in替换ios::out即可

#include <iostream>
#include <fstream>
#include <array>

int main() { // Main will be in prog1_test
    //Open file
    ifstream prog1File("prog1.dat", ios::in);

    //If file can't be opened, exit
    if (!prog1File) {
        cerr << "File could not be opened" << end;
        exit(EXIT_FAILURE);
    }
        int size;
        int numArray[][];
 }
#包括
#包括
#包括
int main(){//main将在prog1_测试中
//打开文件
ifstream prog1文件(“prog1.dat”,ios::in);
//如果无法打开文件,请退出
如果(!prog1File){

cerr只要用ifstream替换ofstream,用ios::in替换ios::out即可

#include <iostream>
#include <fstream>
#include <array>

int main() { // Main will be in prog1_test
    //Open file
    ifstream prog1File("prog1.dat", ios::in);

    //If file can't be opened, exit
    if (!prog1File) {
        cerr << "File could not be opened" << end;
        exit(EXIT_FAILURE);
    }
        int size;
        int numArray[][];
 }
#包括
#包括
#包括
int main(){//main将在prog1_测试中
//打开文件
ifstream prog1文件(“prog1.dat”,ios::in);
//如果无法打开文件,请退出
如果(!prog1File){

cerr只要用ifstream替换ofstream,用ios::in替换ios::out即可

#include <iostream>
#include <fstream>
#include <array>

int main() { // Main will be in prog1_test
    //Open file
    ifstream prog1File("prog1.dat", ios::in);

    //If file can't be opened, exit
    if (!prog1File) {
        cerr << "File could not be opened" << end;
        exit(EXIT_FAILURE);
    }
        int size;
        int numArray[][];
 }
#包括
#包括
#包括
int main(){//main将在prog1_测试中
//打开文件
ifstream prog1文件(“prog1.dat”,ios::in);
//如果无法打开文件,请退出
如果(!prog1File){

cerr我并没有真正理解这个洞的问题。我假设您在创建2D数组并从文件中读取它时遇到了问题。因此,从我的角度来看,下面是一些代码,展示了如何完成此操作:

#include <iostream>
#include <fstream>
#include <vector>


int main() { // Main will be in prog1_test
    //Open file
    ifstream prog1File("prog1.dat");

    //If file can't be opened, exit
    if (!prog1File) {
        cerr << "File could not be opened" << end;
        exit(EXIT_FAILURE);
    }
    int size;

    prog1File >> size;

    int[][] matrix = new int[size][];
    for (int i = 0; i < size; ++i)
    {
        matrix[i] = new int[size];
        for (int j = 0; j < size; ++j)
        {
            prog1File >> matrix[i][j];
        }
    }

    /* do your stuff */

    for (int i = 0; i < size; ++i)
    {
        delete matrix[i];
    }
    delete matrix;

    return 0;
 }
#包括
#包括
#包括
int main(){//main将在prog1_测试中
//打开文件
ifstream prog1File(“prog1.dat”);
//如果无法打开文件,请退出
如果(!prog1File){
cerr大小;
int[][]矩阵=新的int[size][];
对于(int i=0;i>矩阵[i][j];
}
}
/*做你的事*/
对于(int i=0;i
我并不是真的遇到了这个洞的问题。我假设您在创建2D数组并从文件中读取它时遇到了问题。因此,从我的角度来看,下面是一些代码,展示了如何完成此操作:

#include <iostream>
#include <fstream>
#include <vector>


int main() { // Main will be in prog1_test
    //Open file
    ifstream prog1File("prog1.dat");

    //If file can't be opened, exit
    if (!prog1File) {
        cerr << "File could not be opened" << end;
        exit(EXIT_FAILURE);
    }
    int size;

    prog1File >> size;

    int[][] matrix = new int[size][];
    for (int i = 0; i < size; ++i)
    {
        matrix[i] = new int[size];
        for (int j = 0; j < size; ++j)
        {
            prog1File >> matrix[i][j];
        }
    }

    /* do your stuff */

    for (int i = 0; i < size; ++i)
    {
        delete matrix[i];
    }
    delete matrix;

    return 0;
 }
#包括
#包括
#包括
int main(){//main将在prog1_测试中
//打开文件
ifstream prog1File(“prog1.dat”);
//如果无法打开文件,请退出
如果(!prog1File){
cerr大小;
int[][]矩阵=新的int[size][];
对于(int i=0;i>矩阵[i][j];
}
}
/*做你的事*/
对于(int i=0;i
我并不是真的遇到了这个洞的问题。我假设您在创建2D数组并从文件中读取它时遇到了问题。因此,从我的角度来看,下面是一些代码,展示了如何完成此操作:

#include <iostream>
#include <fstream>
#include <vector>


int main() { // Main will be in prog1_test
    //Open file
    ifstream prog1File("prog1.dat");

    //If file can't be opened, exit
    if (!prog1File) {
        cerr << "File could not be opened" << end;
        exit(EXIT_FAILURE);
    }
    int size;

    prog1File >> size;

    int[][] matrix = new int[size][];
    for (int i = 0; i < size; ++i)
    {
        matrix[i] = new int[size];
        for (int j = 0; j < size; ++j)
        {
            prog1File >> matrix[i][j];
        }
    }

    /* do your stuff */

    for (int i = 0; i < size; ++i)
    {
        delete matrix[i];
    }
    delete matrix;

    return 0;
 }
#包括
#包括
#包括
int main(){//main将在prog1_测试中
//打开文件
ifstream prog1File(“prog1.dat”);
//如果无法打开文件,请退出
如果(!prog1File){
cerr大小;
int[][]矩阵=新的int[size][];
对于(int i=0;i>矩阵[i][j];
}
}
/*做你的事*/
对于(int i=0;i
我并不是真的遇到了这个洞的问题。我假设您在创建2D数组并从文件中读取它时遇到了问题。因此,从我的角度来看,下面是一些代码,展示了如何完成此操作:

#include <iostream>
#include <fstream>
#include <vector>


int main() { // Main will be in prog1_test
    //Open file
    ifstream prog1File("prog1.dat");

    //If file can't be opened, exit
    if (!prog1File) {
        cerr << "File could not be opened" << end;
        exit(EXIT_FAILURE);
    }
    int size;

    prog1File >> size;

    int[][] matrix = new int[size][];
    for (int i = 0; i < size; ++i)
    {
        matrix[i] = new int[size];
        for (int j = 0; j < size; ++j)
        {
            prog1File >> matrix[i][j];
        }
    }

    /* do your stuff */

    for (int i = 0; i < size; ++i)
    {
        delete matrix[i];
    }
    delete matrix;

    return 0;
 }
#包括
#包括
#包括
int main(){//main将在prog1_测试中
//打开文件
ifstream prog1File(“prog1.dat”);
//如果无法打开文件,请退出
如果(!prog1File){
cerr大小;
int[][]矩阵=新的int[size][];
对于(int i=0;i>矩阵[i][j];
}
}
/*做你的事*/
对于(int i=0;i