Passing a file as a function reference argument

Hello. I have a problem with a little file program:

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>
#include<string>

int ModifyFile(std::ifstream &OutputFile, std::ofstream &InputFile)
{
    char StringFromFile[7];
    
    if (OutputFile)
    {
        OutputFile << "Read this text";
        OutputFile.close();
    }
    
    if (InputFile)
    {
        InputFile >> StringFromFile;
        
        std::cout << std::endl << StringFromFile << std::endl;
        InputFile.close();
    }
    return 0;
}

int main()
{
    std::string FilePath;
    
    std::cout << "Please enter a file path: ";
    std::cin >> FilePath;
    
    std::ifstream OutputFile (FilePath);
    std::ofstream InputFile (FilePath);
    
    ModifyFile(OutputFile, InputFile);
    return 0;
}


I am trying to create a new file asking user about its path then passing it as an argument to a new in/out - put file and then passing that file as a reference to a ModifyFile function argument in 2 places. Now the main errors the g++ spits out for the ModifyFile are:

main.cpp:11:23: error: no match for ‘operator<<’ in ‘OutputFile << "Read this text"’


and

main.cpp:17:22: error: no match for ‘operator>>’ in ‘InputFile >> StringFromFile’


& for main:

main.cpp:32:39: error: no matching function for call to ‘std::basic_ifstream<char>::basic_ifstream(std::string&)’


and

main.cpp:33:38: error: no matching function for call to ‘std::basic_ofstream<char>::basic_ofstream(std::string&)’


plus other candidates it suggests. Can anyone help me with that program? Thanks!

Janek566
1
2
    std::ifstream OutputFile (FilePath);
    std::ofstream InputFile (FilePath);


If you named your ifstream InputFile and named your ofstream OutputFile, you'd find it a lot easier. Right now your input file object is named OutputFile, and your output file object is named InputFile.
Last edited on
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>
#include<string>

int ModifyFile(std::ifstream &InputFile, std::ofstream &OutputFile)
{
    char StringFromFile[7];
    
    if (InputFile)
    {
        InputFile << "Read this text";
        InputFile.close();
    }
    
    if (OutputFile)
    {
        OutputFile >> StringFromFile;
        
        std::cout << std::endl << StringFromFile << std::endl;
        OutputFile.close();
    }
    return 0;
}

int main()
{
    std::string FilePath;
    
    std::cout << "Please enter a file path: ";
    std::cin >> FilePath;
    
    std::ifstream InputFile (FilePath);
    std::ofstream OutputFile (FilePath);
    
    ModifyFile(InputFile, OutputFile);
    return 0;
}


OK here's the changed version so everyone can read it now. Hope someone can help me with this.
main.cpp:33:38: error: no matching function for call to ‘std::basic_ofstream<char>::basic_ofstream(std::string&)’
plus other candidates it suggests
And those candidates are...
Use FilePath.c_str() as the stream constructor ask for a const char *

You've got your operators backward. They show the flux of data
cin and InputFile are input_streams.
Last edited on
As ne555 indicates, and as I thought might become clear once you renamed the two streams, you're trying to read in from an output, and read out to an input.

InputFile << "Read this text";

So you're trying to pass the text "Read this text" to an input stream. That seems foolish. Surely if you want to pass text to something, you should be passing it to the output stream.

OutputFile >> StringFromFile;

So you're trying to read from an output stream. That seems foolish. Surely if you want to read text from something, you should be reading from an input stream.
Last edited on
I'm really sorry but no matter what I do i get the same bloody compilation errors. Can someone tell me which part of code to swap/change. I looked at 2 versions I've posted and both don't work. If i present one you say it's better to rename it. When I rename it you say to change it back because I mixed Input with the output. Help?

The problem was that you're trying to read in from an output, and write out to an input (also, the constructors for ifstream and ofstream don't take string, they take char*). You must read in from the input, and write out to the output. You gave them exactly the wrong names, which then stopped you being able to think about it.

The following compiles.

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

int ModifyFile(std::ifstream &InputFile, std::ofstream &OutputFile)
{
    char StringFromFile[7];
    
    if (InputFile)
    {
        InputFile >> StringFromFile;
        InputFile.close();
    }
    
    if (OutputFile)
    {
        OutputFile << "Text for outputting";
        OutputFile.close();
    }
    return 0;
}

int main()
{
    std::string FilePath;
    
    std::cout << "Please enter a file path: ";
    std::cin >> FilePath;
    
    std::ifstream InputFile(FilePath.c_str());
    std::ofstream OutputFile(FilePath.c_str());
    
    ModifyFile(InputFile, OutputFile);
    return 0;
}
Last edited on
Ok i see now the mistake I made. Thanks for the help guys!!!
Topic archived. No new replies allowed.