从单独的文本文件中获取数组值

时间:2022-04-01 12:41:39

This is my first attempt at getting a bunch of values from a separate text document. I have a separate text file named P.txt with the following values 13 49 16 2 4 0 90 60 40 20 60 -100 7 1 5 9 3 3 15 -22 11 7 12 3
I have never tried to get data from an outside file to put into an array and while working I keep getting errors on line 15 which is:

这是我第一次尝试从单独的文本文档中获取一堆值。我有一个名为P.txt的单独文本文件,其中包含以下值:13 49 16 2 4 0 90 60 40 20 60 -100 7 1 5 9 3 3 15 -22 11 7 12 3我从未尝试从外部获取数据文件放入一个数组,并在工作时我一直在第15行得到错误,这是:

read_data(int nums[], int size);

Visual studio says that it is expecting a ")" after int and that the read_data function does not take 0 arguments. Can anyone explain these errors to me and help guide me in the right direction? Thanks!

Visual Studio说它在int之后期望一个“)”并且read_data函数不接受0个参数。任何人都可以向我解释这些错误并帮助指导我朝着正确的方向前进吗?谢谢!

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void read_data(int nums[], int size);

int main()
{
    const int size = 24;
    ifstream dataIn;
    double lab[size];

    cout << "The numbers on file are:\n "; 
    read_data(int nums[], int size);

    system("PAUSE");
    return 0;
}

void read_data(int nums[], int size)
{
    ifstream dataIn;
    dataIn.open("P.txt");

    if( dataIn.fail() )
            {
                    cout << "File does not exist." << endl;
                    exit(1);
            } 

    int count;
    for ( count = 0; count < size; count++ )
    {
            dataIn >> nums[count];
}

    dataIn.close(); }

1 个解决方案

#1


1  

The problem is in the line read_data(int nums[],int size);

问题出在read_data(int nums [],int size)行中;

You cannot declare a variable in a function call.

您不能在函数调用中声明变量。

Also, you cannot use system("pause") without stdlib.h or cstdlib

此外,如果没有stdlib.h或cstdlib,则无法使用system(“pause”)

Use this instead :

改为使用它:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>

using namespace std;

void read_data(int nums[], int size);

int main()
{
    const int size = 24;
    ifstream dataIn;
    double lab[size];
    int nums[size];

    cout << "The numbers on file are:\n ";
    read_data(nums, size);

    system("PAUSE");
    return 0;
}

void read_data(int nums[], int size)
{
    ifstream dataIn;
    dataIn.open("P.txt");

    if( dataIn.fail() )
    {
            cout << "File does not exist." << endl;
            exit(1);
    }

    int count;
    for ( count = 0; count < size; count++ )
    {
            dataIn >> nums[count];
}

    dataIn.close(); }

#1


1  

The problem is in the line read_data(int nums[],int size);

问题出在read_data(int nums [],int size)行中;

You cannot declare a variable in a function call.

您不能在函数调用中声明变量。

Also, you cannot use system("pause") without stdlib.h or cstdlib

此外,如果没有stdlib.h或cstdlib,则无法使用system(“pause”)

Use this instead :

改为使用它:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>

using namespace std;

void read_data(int nums[], int size);

int main()
{
    const int size = 24;
    ifstream dataIn;
    double lab[size];
    int nums[size];

    cout << "The numbers on file are:\n ";
    read_data(nums, size);

    system("PAUSE");
    return 0;
}

void read_data(int nums[], int size)
{
    ifstream dataIn;
    dataIn.open("P.txt");

    if( dataIn.fail() )
    {
            cout << "File does not exist." << endl;
            exit(1);
    }

    int count;
    for ( count = 0; count < size; count++ )
    {
            dataIn >> nums[count];
}

    dataIn.close(); }