Ifstream, Functions, Arrays

Hello. Im trying have a program read values from an outside file and put those values into an array using a second function. I'm not sure the right method to call it. Or if i even set it up the correct way.

Thank You for helping.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>

using namespace std;
void readFile(string, int[], int& arrayS);

const int NUMBERS = 100;


int main (){
    
    string getFilename;
    int number[NUMBERS];

    cout << "Enter filename: ";
    cin >> getFilename;
    
    readFile(getFilename, number, arrayS);
    
    cout<<endl;
    cout << number[0];
    
}

void readFile(string getFilename, int number[], int& arrayS){
    int numbers[NUMBERS];

    ifstream file;

    file.open(getFilename.c_str());

        for (int i = 0; i < NUMBERS; i++){
            file >> numbers[i];
        }

    file.close();
}
What is arrayS? At the moment it's not declared in main() so you can't pass it as argument to the function on line 18.

You should remove line 26, and rename number to numbers on line 25.
Last edited on
Hello grjr02,

In addition to what Peter87 has said in your function when you open a file for input you should check and make sure the file is open and that you have a usable stream. Otherwise you could be using a bad stream in the for loop and nothing will be put in your array.

1
2
3
4
5
6
7
8
file.open(getFilename);

if (!file)
{
	std::cout << "\n Your Error massage here" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(5));  // Requires header files "chrono" and "thread"
	exit(1);  // <--- No point in continuing.
}

Also notice the first line. From C++11 on a "std::string" will work. There is no need to convert it to a C string.

FYI in the read function you define int numbers[NUMBERS];. This is fine except that it is a local variable that you read your file into and when the function ends this array is destroyed and you no longer have any access to it.

Backing up to main cin >> getFilename; will work fine if the file name is "FileName", but if it is "File Name" the variable getFilename will only contain "File" as the formatted input will stop at the first white space it finds. a better choice here is std::getline(std::cin, getFilename);. This will get whatever you type including the new line character at the end which is eventually discarded.

Lastly you have an input file that you are using. It is helpful if you post the input file or at least a sample of what you are using because those of us on this end may see something that you do not.

Hope that helps,

Andy
Hello grjr02,

After working with your program there are a couple of things I found:

First you are using a "std::string", but yo did not include the header file <string>.

Line 7 should be above line 5 because the compiler will not see line 7 and you may need it in line 5.

Even though you do not use it or need it you are trying to pass an array to a function by reference, int& arrayS, This is not the correct way. It should look like this int (&array)S[NUMBERS]. When passing by reference you will need to give the dimension(s) a proper size for it to work correctly. And both dimensions are needed for a 2D array.

I did change some parts of the program mostly for testing to make it easier.

Your program will work when the changes are made.

Hope that helps,

Andy
Topic archived. No new replies allowed.