简体   繁体   中英

Problem reading numbers from a file and storing them in an array c++

The goal of my program is to eventually read a file filled with numbers (chosen by the user) into an array and to output the min and max values. However, I cannot get my code to output the correct number of inputs in the file, which I want to use as the size of my array.

The code I have now is

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

int main(){
    string fileName;
    int count(0);
    cout << "Enter input file name: ";
    cin >> fileName;
    ifstream in_file;
    in_file.open(fileName);
    if(in_file.is_open()){
        in_file >> count;
        }
    cout << "The number of entries is " << count;
    int arrayNumbers[count];    
    for (int i = 0; i < count; ++i){
        in_file >> arrayNumbers[i];
        cout << arrayNumbers[i] << endl;
    }
    in_file.close();
}

I have a text file called tester.txt in the same directory as my .cpp file, but it only has 9 entries (1-9 on seperate lines) yet when I cout the count, it says the count is 12. I have seen in other questions like mine the use of

in_file >> count;

to count how many numbers are in a file, but I don't understand what this does since its my first time reading from a file. The file I am trying to read from has the following in it

1
2
3
4
5
6
7
8
9

I have not started the second part of the problem, finding the min and max, however I was just going to sort the array and then display arrayNumber[0] and arrayNumber[count-1] to show the min and max, but first I need to know how large to make the array based on the input file.

but I don't understand what this does

It reads the first number of your ifstream into count .

For the code you have to work as intended, you need to append the total count of numbers to the beginning of the input file so that your file will look like this.

9
1
2
3
4
5
6
7
8
9

thanks for all the advice, I managed to get it working as intended. I'll post my code below.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <iterator>

using namespace std;

string fileName;
int num, counter = 0;

int main(){
    cout << "Enter input file name: ";
    cin >> fileName;
    ifstream in_file;
    in_file.open(fileName);

    while(in_file >> num){
        counter = counter+1;
    }
    in_file.clear();                    //clear the eof flag after reaching end of file
    in_file.seekg (0, ios::beg);        //go back to the start of the file to read
    int arrayNumbers[counter];          
    for (int i = 0; i < counter; ++i){
        in_file >> arrayNumbers[i];
    }
    in_file.close();
    sort(arrayNumbers, arrayNumbers+counter);
    cout << "Min: " << arrayNumbers[0] << endl 
         << "Max: " << arrayNumbers[counter-1];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM