Fstream header file error

Hello, I have been testing out the fstream part of the standard library. I wanted to make a header file, so it is easier to use, so I wrote the file, and a test program to go with it.

Th error I'm getting is:line 7 error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&, ...
It's something similar for every time I make a call to a fstream object

I guess my question is: Can you make a header for fstream? If so, What did I do wrong?

Since I don't think it has to do with the problem, I put up a pastebin for the main code: http://pastebin.com/Cfpi6ELE
Here's the fileIO.h 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
#ifndef FILEIO_H
#define FILEIO_H
#include <string>
#include <fstream>
using namespace std;
int open(string fileName, ifstream fileRead, ofstream fileWrite) {
    fileRead.open(fileName, ios::in);
    fileWrite.open(fileName, ios::app);
    if(fileRead.is_open() && fileWrite.is_open()) {
        return 0;
    }
    else if(fileRead.is_open() && !fileWrite.is_open()) {
        return 1;
    }
    else if(!fileRead.is_opem() && fileWrite.is_open()) {
        return 2;
    }
    else {
        return 3;
    }
};

bool write(ofstream fileWrite, string message) {
    if(!fileWrite.is_open) {
        return false;
    }
    else if(fileWrite.is_open) {
        fileWrite << message << "%" << endl;
        return true;
    }
};

string read(ifstream fileRead) {
    if(fileRead.is_open) {
        string line;
        while(fileRead.good()) {
            getline(fileRead, line);
        }
        return line;
    }
    else if(!fileRead.is_open) {
        return "File_is_not_open";
    }
};

#endif 


NOTE: This is my first post, I hope I'm doing everything good, let me know if there's anything I need to change, or more information I need to give.
void std::ifstream::open ( const char * filename, ios_base::openmode mode = ios_base::in );

It's asking for a const char* and you're trying to give it a string. Use string::c_str() to convert strings into const char*.

Also,
You have a typo on line 15: fileRead.is_opem
You forgot the parenthesis to the function calls on lines 24, 27, 34 and 41.

You are also making redundant checks in your if/else statements.
1
2
3
4
if(x)
    ...
else // if(!x) is implied here
    ...
Last edited on
First of all I think you should pass the streams by reference. That is instead of

int open(string fileName, ifstream fileRead, ofstream fileWrite) {

you should write

int open(string fileName, ifstream &fileRead, ofstream &fileWrite) {

As for the error then you compiler does not support the function

fileRead.open(fileName, ios::in);

with such arguments. Try to use

fileRead.open(fileName.c_str(), ios::in);
ifstream doesn't have copy constructor.
Pass the stream variables as references.

For an instance replace string read(ifstream fileRead) with string read(ifstream& fileRead)

Thank you all for your help. As I am not a master with pointers and references. Am I correct that in the declaration, and usage I would pass the variables with the (&) symbol to pass the memory adress, and when accessing the variables inside the function (e.g. fileRead.open(fileName, ios::in);), I would use the (*) symbol to access what's at memory adress &fileName (So, fileRead.open(*fileName, ios::in);)

That's me trying to understand and rephrase pointers.

As for you BranFlakes, would I call open as open(fileName.c_str(), fileRead, fileWrite); and a personal thanks for spotting those typo's.

So, I might have more problems here, but not for a couple days.
(So, fileRead.open(*fileName, ios::in);)

No, not unless fileName is a char**. fileName is a string, so it will be fileRead.open(fileName.c_str(), ios::in);
Besides, no one ever said to pass fileName as a pointer.

We're telling you to pass fileRead and fileWrite by reference, which means changing the function to what vlad wrote above your post.

References are used as if they were normal variables, so no dereference operators are necessary.
Last edited on
Topic archived. No new replies allowed.