-2

Making a program that reads integers from a file and creates an array, I have that part completed however I am trying to figure out how to change the SIZE value depending on how many ints are in the file. This file has 15 integers but another file may have more and this array will not take in all the ints.

using namespace std;

const int SIZE = 15;
int intArray[SIZE];

void readData(istream& inFile) {
    for (int i = 0; i < SIZE; i++){
        inFile >> intArray[i];
        cout << intArray[i] << " ";
    }
}

int main() {
    ifstream inFile;
    string inFileName = "intValues.txt";
    inFile.open(inFileName.c_str());
    
    int value, count = 0;
    while(inFile >> value){
        count += 1;
    }
    
    cout << count;
    readData(inFile);
    return 0;
}

As you can see I have a while loop counting the number of ints in the file however when I assign that to the size value I was running into many different issues.

TimJones
  • 11
  • 2
  • 12
    what you need is std::vector – Borgleader Oct 25 '21 at 23:07
  • 1
    Cannot be done. Once an array has been sized, that size cannot change. What you can do is dynamically allocate an array with a fixed size. Then when you have filled the array, create a new larger array, copy the contents of old array into the new array, free the old array and set the new array as the old array. Repeat each time the array fills up. This is one of the many things `std::vector` does for you. – user4581301 Oct 25 '21 at 23:07
  • 1
    This seems to be an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You need a way to store an unknown number of things. Why must the solution involve a fixed size array? – Drew Dormann Oct 25 '21 at 23:10
  • I respectfully disagree that this question is a duplicate of [How do I use arrays in C++?](https://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) Reopened. – Drew Dormann Oct 25 '21 at 23:13
  • You can't. The size of an array is required to be a compile-time constant in C++ so, by definition, the size cannot change based on runtime input (e.g. from a file). Instead of trying to resize an array, try using a resizeable container - such as `std::vector` - which is designed to manage a collection of objects (or a set of values in the case of `int`) where the number of objects is determined at run time. – Peter Oct 26 '21 at 00:58

1 Answers1

1

A fixed-sized array simply cannot be resized, period. If you need an array whose size can change at runtime, use std::vector instead.

More importantly, you are reading through the entire file just to count the number of integers, and then you are trying to read the values from where the previous loop left off. You are not seeking the ifstream back to the beginning of the file so you can re-read what you have already read.

Try something more like this instead:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

int main() {
    string inFileName = "intValues.txt";

    ifstream inFile(inFileName.c_str());

    int value, count = 0;
    while (inFile >> value){
        ++count;
    }
    
    cout << count;

    std::vector<int> intArray;
    intArray.reserve(count);

    inFile.seekg(0);

    while (inFile >> value){
        intArray.push_back(value);
        cout << value << " ";
    }

    // use intArray as needed...

    return 0;
}

Alternatively, don't even bother counting the integers, just let the std::vector grow as needed, eg:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

int main() {
    string inFileName = "intValues.txt";

    ifstream inFile(inFileName.c_str());

    vector<int> intArray;

    int value;
    while (inFile >> value){
        intArray.push_back(value);
        cout << value << " ";
    }

    // use intArray as needed...
    // you an get the count from intArray.size()

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770