【问题标题】:How do I put integer values from an external file into an array to list each value as an output? C++如何将外部文件中的整数值放入数组中以将每个值列为输出? C++
【发布时间】:2022-01-02 05:14:08
【问题描述】:

我正在尝试进行分配,在该文件中我生成一个具有随机整数值的文件,然后将这些值放入一个数组中,但是,我不太确定如何执行此操作,因为我只收到了错误与我如何尝试这样做,所以我想知道是否有不同的方法来完成这项任务。我还附上了我的代码和带有随机整数的外部文本文件,您可能会注意到还有一些其他功能,但这些功能仅用于分配中的其他要求并创建 txt 文件。

#include <fstream>
#include <cstdlib>
using namespace std;
//function 1 Create File with rand numbers
void createfile() {
    srand(time(0));
    int counter = 0;
    int generatednum = 0;
    ofstream randNums("randomnumbers.txt");
    for (int counter = 0; counter < 50; counter++) {
        int generatednum = rand() % 105 + 2;
        randNums << generatednum << endl;
    }
    randNums.close();
} 

//function 2 count number of apples picked under 25
void countundertwentyfive() {
    int under25count;
    int under25amount = 0;
    ifstream infile;
    infile.open("randomnumbers.txt");
    if (infile.fail())
    {
        cerr << "File failed to open:inputfile.txt";
        abort();
    }
    
    while (!infile.eof()) {
        infile >> under25count;
        if (under25count < 25) {
            under25amount++;
        }
    }
    cout << under25amount << " people picked under 25 apples." << endl;
    infile.close();
}

//function 3 count number of apples picked btwn 80 and 100
void btwn80and100() {
    int eightyand100apples;
    int under80and100amount = 0;
    ifstream infile;
    infile.open("randomnumbers.txt");
    if (infile.fail())
    {
        cerr << "File failed to open:inputfile.txt";
        abort();
    }

    while (!infile.eof()) {
        infile >> eightyand100apples;
        if (eightyand100apples >= 80 && eightyand100apples <= 100) {
            under80and100amount++;
        }
    }
    cout << under80and100amount << " people picked between 80 and 100 apples." << endl;
    infile.close();
}

//function 4 Find average number of apples picked 
void averageapplepicked() {
    int numbergetter;
    int sum = 0;
    ifstream infile;
    infile.open("randomnumbers.txt");
    if (infile.fail())
    {
        cerr << "File failed to open:inputfile.txt";
        abort();
    }

    while (!infile.eof()) {
        infile >> numbergetter;
        sum += numbergetter;
    }
    sum /= 50;
    cout << "Approximately " << sum << " apples were picked on average" << endl;
    infile.close();
}

//Use of previous functions along with the array thing I dont understand how to do
int main() {
    createfile();
    countundertwentyfive();
    btwn80and100();
    averageapplepicked();

    int arrayvalues;
    int arraydisplay;
    ifstream infile;
    infile.open("randomnumbers.txt");
    if (infile.fail())
    {
        cerr << "File failed to open:inputfile.txt";
        abort();
    }
    while (!infile.eof()) {
        int arraydisplay[50] = { infile >> arrayvalues };
    }
    cout << arraydisplay[50] << endl;
    infile.close();


    return 0;
} ```

The txt file generated by the code 

    3
    
    74
    
    89
    
    20
    
    35
    
    79
    
    12
    
    12
    
    82
    
    98
    
    25
    
    5
    
    59
    
    99
    
    54
    
    46
    
    53
    
    38
    
    98
    
    42
    
    103
    
    55
    
    73
    
    96
    
    
    98
    
    75
    
    48
    
    42
    
    70
    
    82
    
    50
    
    62
    
    26
    
    90
    
    17
    
    103
    
    24
    
    56
    
    93
    
    46
    
    10
    
    49
    
    33
    
    76
    
    40
    
    44
    
    6
    
    101
    
    43
    
    43

Thank you so much for any help!

【问题讨论】:

标签: c++ arrays


【解决方案1】:

从文件中读取相当容易。

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

int main() {
    std::fstream newfile("random-numbers.txt", std::ios::in);
    if (!newfile.is_open()) {
       // Handle file not opening properly
    }
   
    std::vector<int> random_nums;
    std::string data;
    while(getline(newfile, data)){ // Read line by line
        random_nums.push_back(std::stoi(data));
    }
    newfile.close(); // Close the file
}

我在这里使用std::vector,以防您不知道文件的长度。一旦你有了你的向量,你就可以对每个值进行操作,或者随心所欲地迭代它。

【讨论】:

  • 我明白了,但是如何将它读入数组?
  • @tgol 查看答案here。你会做类似int* numbers = &amp;random_nums[0];
猜你喜欢
  • 2014-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多