Passing fstream to a function, not working. can it?

I want to pass an input file to a function and within the function, fill an array by reference. My data file is just 90 characters. When I try running the file the compiler opens up another file called, io_base.h. I know the array and the data file are working fine because when I simply open the file and read the info into the array it works fine, just when I try passing the fstream input file things get wiggy. I couldn't really find anything about this in my book - can someone tell me if it can be done and if so, how?

This only happens when I try and run the program, it compiles just fine. I think there's something I'm not understanding about the fstream object, and how it works as opposed to regular variables. Like I said the books I have never covered this, or at least I couldn't find any mention of it.

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
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <fstream>

using namespace std;

// ------ Prototype
void readFile(char [][30], ifstream);


// ****** MAIN ****************************************************************
int main()
{
    char weather[3][30];
    ifstream inputFile("P5.txt");

    readFile(weather, inputFile);

        for(int i = 0; i < 3; i++)
        {
            for(int k = 0; k < 30; k++)
            {
            cout << weather[i][k];
            if(k == 29)
            {
                cout << endl;
            }
        }
    }

    return 0;
}

// ------ FUNCTION ------------------------------------------------------------
//
// ----------------------------------------------------------------------------
void readFile(char weather[][30], ifstream inputFile)
{
    for(int i = 0; i < 3; i++)
    {
        for(int k = 0; k < 30; k++)
        {
            inputFile >> weather[i][k];
        }
    }


    return;
}
Pass by reference:

6
7
// ------ Prototype
void readFile(char [][30], ifstream&);
35
36
// ----------------------------------------------------------------------------
void readFile(char weather[][30], ifstream& inputFile)

Hope this helps.
Thanks for the reply, and this is consistent with what I've found on the net, but for some reason it's just not getting the file and printing garbage.

This is what I just tried, and from what I can tell should work...am I missing something? Thanks again for any help, I hate when something should work but doesn't, drives me bonkers.
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
38
39
40
41
42
43
44
45
46
47
48
49
50

#include <iostream>
#include <fstream>

using namespace std;

// ------ Prototype
void readFile(char [][30], ifstream&);


// ****** MAIN ****************************************************************
int main()
{
    char weather[3][30];
    ifstream inputFile("P5.txt");

    readFile(weather, inputFile);

        for(int i = 0; i < 3; i++)
        {
            for(int k = 0; k < 30; k++)
            {
            cout << weather[i][k];
            if(k == 29)
            {
                cout << endl;
            }
        }
    }

    return 0;
}

// ------ FUNCTION ------------------------------------------------------------
//
// ----------------------------------------------------------------------------
void readFile(char weather[][30], ifstream &inputFile)
{
    for(int i = 0; i < 3; i++)
    {
        for(int k = 0; k < 30; k++)
        {
            inputFile >> weather[i][k];
        }
    }


    return;
}


This is what I keep getting:

(∟G Φ■( ▌µ@ ♠ !G ┤═G
(∟G ┤═G ▄²( ªuA ►╨F <■( ☺ ┤═
G ♠ ♠ ► ☺ P↓@ ▄╗F

Process returned 0 (0x0) execution time : 0.070 s
Press any key to continue.
I ran your code, it worked fine for me. You should check to make sure the file opened properly.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

// ------ Prototype
void readFile(char[][30], ifstream&);


// ****** MAIN ****************************************************************
int main()
{
	char weather[3][30];
	ifstream inputFile("P5.txt");
	
	if (!inputFile)
	{
		cout << "Error P5.txt failed to open program terminated";
		cin.ignore();
		exit(1);
	}

	readFile(weather, inputFile);

	for (int i = 0; i < 3; i++)
	{
		for (int k = 0; k < 30; k++)
		{
			cout << weather[i][k];
			if (k == 29)
			{
				cout << endl;
			}
		}
	}

	cin.ignore();
	return 0;
}

// ------ FUNCTION ------------------------------------------------------------
//
// ----------------------------------------------------------------------------
void readFile(char weather[][30], ifstream &inputFile)
{
	for (int i = 0; i < 3; i++)
	{
		for (int k = 0; k < 30; k++)
		{
			inputFile >> weather[i][k];
		}
	}


	return;
}
Last edited on
It seems to be opening just fine, but somewhere something is getting lost between main and the function.

Thanks either way, you've been very helpful!

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <fstream>

using namespace std;

// ------ Prototype
void readFile(char [][30], ifstream &);


// ****** MAIN ****************************************************************
int main()
{
    char weather[3][30];
    ifstream inputFile("P5.txt");

    if(inputFile)
    {
        for(int i = 0; i < 3; i++)
        {
            for(int k = 0; k < 30; k++)
            {
                cout << weather[i][k];
                if(k == 29)
                {
                    cout << endl;
                }
            }
        }
    }
    else
    {
        cout << "open error";
    }
    readFile(weather, inputFile);




    return 0;
}

// ------ FUNCTION ------------------------------------------------------------
//
// ----------------------------------------------------------------------------
void readFile(char weather[][30], ifstream &inputFile)
{
    for(int i = 0; i < 3; i++)
    {
        for(int k = 0; k < 30; k++)
        {
            inputFile >> weather[i][k];
        }
    }


    return;
}
Last edited on
OK, so you think that time flows that way, do you? Interesting.
I'm not 100% sure of the cause of your original problem, but I think it's caused by the call taking a copy of the ifstream object with a default copy constructor and the destructor being called for that object on return, closing the file or not updating the original ifstream object.

I think this is no longer possible with gcc4.7 and it's library because it uses the =delete for the copy constructor, preventing the compiler from doing it's own copy constructor.
In: http://en.cppreference.com/w/cpp/io/basic_ifstream/basic_ifstream:

basic_ifstream( const basic_ifstream& rhs) = delete; (since C++11)

With the original code my gcc4.7 gives:

121157.cpp:35:30: error: use of deleted function ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’
In file included from 121157.cpp:2:0:
/usr/include/c++/4.7/fstream:420:11: note: ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.7/fstream:420:11: error: use of deleted function ‘std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)’


Ref: http://stackoverflow.com/questions/12432952/why-is-my-fstream-being-implicitly-deleted

As I say, I'm not real sure on this explanation but using a reference seems the way to go.
Last edited on
You display the contents of the "weather" array before you send it to "readFile". Its displaying the contents of an uninitialized array (which is random.)
Just move :
readFile(weather, inputFile);
to be before the "for" loop:
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <fstream>

using namespace std;

// ------ Prototype
void readFile(char [][30], ifstream &);


// ****** MAIN ****************************************************************
int main()
{
    char weather[3][30];
    ifstream inputFile("P5.txt");

    readFile(weather, inputFile); // before the loop

    if(inputFile)
    {
        for(int i = 0; i < 3; i++)
        {
            for(int k = 0; k < 30; k++)
            {
                cout << weather[i][k];
                if(k == 29)
                {
                    cout << endl;
                }
            }
        }
    }
    else
    {
        cout << "open error";
    }




    return 0;
}

// ------ FUNCTION ------------------------------------------------------------
//
// ----------------------------------------------------------------------------
void readFile(char weather[][30], ifstream &inputFile)
{
    for(int i = 0; i < 3; i++)
    {
        for(int k = 0; k < 30; k++)
        {
            inputFile >> weather[i][k];
        }
    }


    return;
}


Other than that your code is fine, and you didn't make this mistake in the original code you showed, so tell me if it solved it or not..
Last edited on

#include <iostream>
#include <fstream>

using namespace std;

// ------ Prototype
void readFile(char [][30], ifstream);


// ****** MAIN ****************************************************************
int main()
{
char weather[3][30];
ifstream inputFile("P5.txt");

readFile(weather, inputFile);

for(int i = 0; i < 3; i++)
{
for(int k = 0; k < 30; k++)
{
cout << weather[i][k];
if(k == 29)
{
cout << endl;
}
}
}

return 0;
}

// ------ FUNCTION ------------------------------------------------------------
//
// ----------------------------------------------------------------------------
void readFile(char weather[][30], ifstream inputFile)
{
for(int i = 0; i < 3; i++)
{
for(int k = 0; k < 30; k++)
{
inputFile >> weather[i][k];
}
}


return;
}




I get an error for readFile (weather, inputFile);

says: initializing argument 2 of 'void readFile(char(*)(30], std::if stream)'
You're having problems with sending "ifstream" by-value. just change it to:
void readFile(char [][30], ifstream &);
And:
void readFile(char weather[][30], ifstream &inputFile)
Now when it's sent by reference, it should work fine..
AH! Thanks guys, it's working today. After some messing around, I'm pretty sure I had a wrong .dat file name somewhere. Great learning experience, thanks for the help!
Topic archived. No new replies allowed.