<fstream> error at build (error code in post)

Hello experts and thanks again in advance for your help!! I'm doing a few lessons here and I've been required to write a very basic program to write lines to a text file until the user signals to stop. The example that I'm modifying uses char[] arrays? for the lines but seeing as how the book just went over the newer string class I figured I'd also update it to use that while completing the exercise.

for some reason I'm getting and issue opening the file and the debugger is pointing to line 33 where I attempt to open the file via the modified string.

I feel like there is something wrong with the string that I'm passing to the ofstream function but I don't think I have enough knowledge of how it works to understand whats wrong. I have created another program that wrote to a file but the filename passed to ofstream was coded inline rather than as a variable based on user input. Below is the error coming from the compiler.


error: no matching function for call to 'std::basic_ofstream<char>::basic_ofstream(std::string&)'

It then lists "possible candidates" but again I don't feel like I know enough to understand what it's telling me. =[

Thanks again for your prospective help!!

I actually enjoy debugging the programs myself but every so often i get something like this. I've not finished checking this one yet, this is just as far as I've gotten, if your expert eye happens to catch something else, don't even sweat it, I like to try to find all the ones I can by myself in the hopes that someday I'll be a pro. Thanks again guys.

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

using namespace std;


int main()
{
    string current, filenm, fileloc, fileinfo, ans1 = "n", ans2 = "y";
    int lines = 0;

    while(ans1 == "n" || ans1 == "N")
    {
        cout << "\n\nWelcome to Textfile Writer 1.0. What would you like to name your file?\n (file extension will be added automatically): ";
        getline(cin, filenm);
        cout << "\n\nPlease enter the path where you'd like to store it: ";
        getline(cin, fileloc);

        fileinfo = fileloc + "\\" + filenm + ".txt";

        correctcheck:
        cout << "\n\nThe full path and filename are: " << fileinfo << "\n\nIs this correct? (y/n) ";
        getline(cin, ans1);

        if(ans1 != "y" || ans1 != "Y" || ans1 != "N" || ans1 != "n")
            {
                cout << "\n\nInvalid answer: " << ans1;
                goto correctcheck;
            }
    }

    ofstream file1(fileinfo);

    cout << "\n\nEnter lines of text to be written to file, each ENTER is a new line, a single space + ENTER will end the session";

    while(ans2 != " ")
    {
        getline(cin, current);
        file1 << current << endl;
        lines++;
    }

    cout << "\n\n" << lines << " line(s) written successfully!\n\n";


    return 0;
}
Last edited on
In order to use a std::string in the file stream constructor you nee to insure your compiler is compiling a C++11 program. Otherwise you need to use the c_str() member function to convert the string to a C-string.

I'm using the latest version of code::blocks with the included compiler. I'll have to check the settings to see if I can maybe change it to '11? Otherwise i would change my line 33 to look something like this:

ofstream file1(c_str(fileinfo)) <---???


I tried it as above and it takes away most of the error, now it just says i need to declare c_str in this scope, wait i think i got it, #include <string.h>??
nope wasn't that one, what do I need to include in order to get the definition of c_str()? i'm sure I can find it online. Thanks for the HELP!!
Last edited on
It's class (dot) syntax, so you would do my_string.c_str() (edit: typo)

Settings -> Compiler Settings -> Compiler Flags (tab) -> "Have g++ follow the C++11 ISO C++ language standard" (check box) for code blocks to enable C++11.
Last edited on
So I'd be looking for?

ofstream file1(fileinfo.c_str()) ?

I will definitely just change the setting to make my life easier thanks for that, now I have s some odd need to see it work with the old conversion though lol.

edit: yep that worked thanks a million!!
Last edited on
Topic archived. No new replies allowed.