为什么我的程序停止工作了?我的指示是错的吗?

Why has my program stopped working? Are my pointers wrong?

本文关键字:我的 指示 停止工作 为什么 程序      更新时间:2023-10-16

我正在编写一个程序,它可以从如下格式的文件中读取一些数据:

21
285 270 272 126 160 103 1
31 
198 180 163 89 94 47 1 
32
240 230 208 179 163 104 1
33
15 13 12 14 15 15 0
34
63 61 62 24 23 20 2

我试图读取第一个数字到一个指针数组和其他7个数字到一个并行的二维指针数组,但由于某种原因,每次我运行我的代码它只是停止工作。它没有返回一个错误,但我觉得我的指针使用是错误的,因为这是我第一次使用指针。数据文件名为"election_data_121.txt"。下面是代码。如果有人能看一看,我将非常感激:

#include <iostream>
#include <fstream>
using namespace std;
//bool openFileIn(fstream &, char *);
int main()
{
    const int PREC_SIZE = 30;
    const int CANIDATES = 7;
    int *precinct_num[PREC_SIZE];
    int *num_votes[PREC_SIZE][CANIDATES];
    cout << "Declarations made." << endl;
    fstream dataFile; //Make a file handle
    cout << "File object made." << endl;
    //Open the file and check that it opened correctly
    if(!openFileIn(dataFile, "election_data_121.txt"))
    {
        cout << "File open error!" << endl;
        return 0; //Exit the program
    }
    cout << "File opened." << endl;
    //Read the contents of the file into the proper arrays
    int counter = 0;
    while(!dataFile.eof()) 
    {
        dataFile >> *precinct_num[counter];
        for(int i = 0; i < 7; i++)
        {
            dataFile >> *num_votes[counter][i];
        }
        counter++;
    }
    //Print out the data
    for(int j = 0; j < counter; j++)
    {
        cout << *precinct_num[j];
        for(int i = 0; i < 7; i++)
        {
            cout << *num_votes[j][i];
        }
    }
    dataFile.close();
    cout << "End of file";
    return 0;
}
bool openFileIn(fstream &file, char *name)
{
    file.open(name, ios::in);
    if(file.fail())
        return false;
    else
        return true;
}

再次感谢!

这段代码根本不需要指针;你为什么认为是这样呢?只要改变precinct_numnum_votes的类型,并停止对它们的解引用,我认为(乍一看)应该没问题。

#include <iostream>
#include <fstream>
using namespace std;
//bool openFileIn(fstream &, char *);
int main()
{
    const int PREC_SIZE = 30;
    const int CANIDATES = 7;
    int precinct_num[PREC_SIZE];
    int num_votes[PREC_SIZE][CANIDATES];
    cout << "Declarations made." << endl;
    fstream dataFile; //Make a file handle
    cout << "File object made." << endl;
    //Open the file and check that it opened correctly
    if(!openFileIn(dataFile, "election_data_121.txt"))
    {
        cout << "File open error!" << endl;
        return 0; //Exit the program
    }
    cout << "File opened." << endl;
    //Read the contents of the file into the proper arrays
    int counter = 0;
    while(!dataFile.eof()) 
    {
        dataFile >> precinct_num[counter];
        for(int i = 0; i < 7; i++)
        {
            dataFile >> num_votes[counter][i];
        }
        counter++;
    }
    //Print out the data
    for(int j = 0; j < counter; j++)
    {
        cout << precinct_num[j];
        for(int i = 0; i < 7; i++)
        {
            cout << num_votes[j][i];
        }
    }
    dataFile.close();
    cout << "End of file";
    return 0;
}
bool openFileIn(fstream &file, char *name)
{
    file.open(name, ios::in);
    return !file.fail();
}

是的,看起来你对指针的理解有缺陷。我不明白你为什么在这里使用指针语法。只要删除指针定义和解引用,它应该做你想要的。你正在声明一个指向整型的指针数组。也就是说,数组的每个元素都包含一个指向整型而非整型的指针。您的代码似乎希望每个数组元素都包含一个int类型。

我建议你找一本关于c++指针的好教程。

首先需要为precinct_num和num_votes分配一些空间。要么使用malloc/calloc,要么直接把它放在堆栈上

您应该更改precinct_num和num_votes变量的声明。现在它们是指向数组的指针。你遇到的错误是这些指针没有指向任何东西!所以你不需要指针指向数组。你只需要直接得到数组。所以改成:

int precinct_num[PREC_SIZE];
int num_votes[PREC_SIZE][CANIDATES];

然后,当你读入值时,你只需要:

dataFile >> precinct_num[counter];

dataFile >> num_votes[counter][i];

等等……

这只是指针方面。我没有查看代码的总体情况,看看它是否在做正确的事情。