calling function using stream

We just started working with stream in my class and I have to write a program that defines a function that is called with the two input-file streams and the output-file stream as three arguments. I haven't been able to find the syntax to do this with even though I have searched a lot.
On line 59:
sortmerger("Sorted_numbers_1", "Sorted_numbers_2", "mergedsort");

I get the error:
could not convert '(const char*)"Sorted_numbers_1"' from 'const char*' to 'std::ifstream' {aka std::basic_ifstream<char>}'

I think what I need to do is declare the stream as int and not char, but I haven't been able to find the way to do that using function. Please help me figure out what I am doing wrong. Thank you in advance.

The goal is to merge two files of numbers in sorted order and write them to a file named mergedsort.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <fstream>
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
#include <iostream>
#include <cstdlib>

using namespace std;

int sortmerger(ifstream sorted_numbers_1, ifstream sorted_numbers_2, ofstream mergedsort);
//program should define a function that is called with the two input-file
//streams and the output-file stream as three arguments.

int main( )
{
    ifstream in_stream;
    ofstream out_stream;
    
    //Create a file called numbers.txt using random number generator
    //to contain 20 integers. The range for these will be 100 - 200
    /* initialize random seed: */
    srand (time(NULL));
    int randnum, i;
    
    out_stream.open("16numbers.dat");
    if (out_stream.fail( ))
    {
        cout << "Output file failed to open.\n";
        exit (1);
    }
    
    // generate 16 random integers within the range of 100- 200
    for (i = 0; i < 16; ++i)
    {
        randnum = rand() % 200 + 100;
        out_stream << randnum << " ";
    }
    out_stream.close( );
    
    out_stream.open("20numbers.dat");
    if (out_stream.fail( ))
    {
        cout << "Output file failed to open.\n";
        exit (1);
    }
    
    // generate 20 random integers within the range of 1- 100
    for (i = 0; i < 20; ++i)
    {
        randnum = rand() % 200 + 100;
        out_stream << randnum << " ";
    }
    out_stream.close( );
    
    in_stream.open("Sorted_numbers_1");
    in_stream.open("Sorted_numbers_2");
    out_stream.open("mergedsort");
    
    sortmerger("Sorted_numbers_1",  "Sorted_numbers_2", "mergedsort");
    return 0;    
}

int sortmerger(ifstream Sorted_numbers_1, ifstream Sorted_numbers_2, ofstream mergedsort)
{
    ifstream in_stream;
    ofstream out_stream;
    in_stream.open("Sorted_numbers_1");
    if (in_stream.fail())
    {
        cout << "input file open failed: Sorted_numbers_1.\n";
        exit (1);
    }
    in_stream.open("Sorted_numbers_2");
    if (in_stream.fail())
    {
        cout << "input file open failed: Sorted_numbers_2.\n";
        exit (1);
    }
    out_stream.open("mergedsort.dat");
    if (out_stream.fail( ))
    {
        cout << "Output file failed to open.\n";
        exit (1);
    }
    return 0;
}
Last edited on
You are calling sortmerger in your main function with char arrays instead of the streams you declared earlier.
You need to call it with two istreams and one ostream, as it was declared.
You might want to call like this, if I understood what your asking:
1
2
3
4
5
6
7
8
9
10
11
ifstream in_stream1;
ifstream in_stream2;
ofstream out_stream;

in_stream1.open("Sorted_numbers_1");
in_stream2.open("Sorted_numbers_2");
out_stream.open("mergedsort");

//check for failures.

sortmerger(in_stream1,  in_stream2,  out_stream);


Alternatively you could redeclare the sortmerger to accept char arrays, and open them inside the function:
1
2
3
4
5
6
7
8
9
10
int sortmerger(char * Sorted_numbers_1, char * Sorted_numbers_2, char * mergedsort)
{
    ifstream in_stream1;
    ifstream in_stream2;
    ofstream out_stream;

    in_stream1.open(Sorted_numbers_1);
    in_stream2.open(Sorted_numbers_2);
    out_stream.open(mergedsort);
}
Thank you phanalax.
So if I understand correctly, the correct syntax is like calling the streams by referring to them, not by literal file name?
We haven't covered arrays yet, and this teacher is really punitive. If I try to use anything we haven't covered in class, he will accuse me of cheating. Makes no sense to me, but that is what he does.

Ok, well... I implemented the first set of changes now I get this:

.../main.cpp:64: error: use of deleted function 'std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)'
sortmerger(in_stream1, in_stream2, out_stream);
^
So I changed it, and get this:

/home/eliza/cpp_projects/Labs/12/pg369prob7/main.cpp:64: error: could not convert '(const char*)"Sorted_numbers_1"' from 'const char*' to 'std::ifstream {aka std::basic_ifstream<char>}'
sortmerger("Sorted_numbers_1", "Sorted_numbers_2", "mergedsort");
^

I am unclear how to call the streams when invoking the function

this is now what I have, apparently the teacher put in some incorrect instructions for the assignment, so I have commented out that code


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 <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;

int sortmerger(ifstream sorted_numbers_1, ifstream sorted_numbers_2, ofstream mergedsort);
//program should define a function that is called with the two input-file
//streams and the output-file stream as three arguments.

int main( )
{
    ifstream in_stream1;
    ifstream in_stream2;
    ofstream out_stream;
    in_stream1.open("Sorted_numbers_1");
    in_stream2.open("Sorted_numbers_2");
    out_stream.open("mergedsort");  

    sortmerger(in_stream1,  in_stream2,  out_stream);
    //sortmerger("Sorted_numbers_1",  in_stream2,  out_stream);
    return 0;
}

//int sortmerger(ifstream Sorted_numbers_1, ifstream Sorted_numbers_2, ofstream mergedsort)
int sortmerger(ifstream in_stream1, ifstream in_stream2, ofstream out_stream)
{
    ifstream Sorted_numbers_1;
    ifstream Sorted_numbers_2;
    ofstream mergedsort;
    in_stream1.open("Sorted_numbers_1");
    in_stream2.open("Sorted_numbers_2");
    out_stream.open("mergedsort");

    if (in_stream1.fail())
    {
        cout << "input file open failed: Sorted_numbers_1.\n";
        exit (1);
    }

    in_stream2.open("Sorted_numbers_2");
    if (in_stream2.fail())
    {
        cout << "input file open failed: Sorted_numbers_2.\n";
        exit (1);
    }

    out_stream.open("mergedsort.dat");
    if (out_stream.fail( ))
    {
        cout << "Output file failed to open.\n";
        exit (1);
    }

    return 0;
}
Last edited on
Have you covered references yet? The problem here is that the stream classes don't have copy constructors, so you must pass them by reference. I did not realize that when I wrote my first answer.
Not entirely sure what you mean by references @phanalax... We have covered functions with call by reference, if that is what you are referring to. I don't believe we have covered passing streams by reference.
We have not covered copy constructors
You don't have to worry about the copy constructors, just set up the sortmerger function to call by reference and the compiler errors should go away.
1
2
3
int sortmerger(ifstream & Sorted_numbers_1, ifstream & Sorted_numbers_2, ofstream & mergedsort)
{
}

you don't need to redeclare the ifstreams and the ofstream inside the sortmerger function, just use the ones passed in.:
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
//changed the function declaration to use call by reference:
int sortmerger(ifstream & in_stream1, ifstream & in_stream2, ofstream & out_stream)
{
/*
These are not necessary:
ifstream Sorted_numbers_1; 
ifstream Sorted_numbers_2;
ofstream mergedsort;
in_stream1.open("Sorted_numbers_1");
in_stream2.open("Sorted_numbers_2");
out_stream.open("mergedsort");
 in_stream.open("Sorted_numbers_1");
if (in_stream1.fail())
{
cout << "input file open failed: Sorted_numbers_1.\n";
exit (1);
}

in_stream2.open("Sorted_numbers_2");
if (in_stream2.fail())
{
cout << "input file open failed: Sorted_numbers_2.\n";
exit (1);
}

out_stream.open("mergedsort.dat");
if (out_stream.fail( ))
{
cout << "Output file failed to open.\n";
exit (1);
}
*/
//You can use the ones declared in the functions arguments:
//in_stream1, in_stream2, out_stream

//Use them to get the file input and then do the merging.
return 0;
}
Last edited on
Awesome! Thanks @phanalax!

Ok... well.. now my files are not opening. I am using Qt. I observed that when I created a file, Qt put it in the build folder, so I first tried putting the provided files there. Not opening.

So next I put them in the same directory as my .cpp file. Still not opening.

My sense is that I have them in the wrong place, but it is not clear to me how I would even know where to put them, or Qt looks for them, or... I asked about this in class, he did not give a clear response.

output:
input file open failed: Sorted_numbers_1.
input file open failed: Sorted_numbers_2.
Press <RETURN> to close this window...


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
#include <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;

int sortmerger(ifstream & Sorted_numbers_1, ifstream & Sorted_numbers_2, ofstream & mergedsort);
//program should define a function that is called with the two input-file
//streams and the output-file stream as three arguments.

int main( )
{
    ifstream in_stream1;
    ifstream in_stream2;
    ofstream out_stream;
    in_stream1.open("Sorted_numbers_1");
    in_stream2.open("Sorted_numbers_2");
    out_stream.open("mergedsort");
    if (in_stream1.fail())
    {
        cout << "input file open failed: Sorted_numbers_1.\n";
        //exit (1);
    }

    in_stream2.open("Sorted_numbers_2");
    if (in_stream2.fail())
    {
        cout << "input file open failed: Sorted_numbers_2.\n";
        exit (1);
    }

    out_stream.open("mergedsort.dat");
    if (out_stream.fail( ))
    {
        cout << "Output file failed to open.\n";
        exit (1);
    }

    sortmerger(in_stream1,  in_stream2,  out_stream);
    return 0;
}

int sortmerger(ifstream &in_stream1, ifstream &in_stream2, ofstream &out_stream)
{
// will get to this when I get my i/o working
    return 0;
}
Last edited on
You need to put them in the same directory as the executable. You can also type the full address of the file.
Thanks. I had it there, didn't realize I needed to call out the .dat extension. Thanks for the help, got the program finished and running @phanalax
Topic archived. No new replies allowed.