.open() doesn't open files

Hi!
I have a strange problem with files. I used method .open() to open file, but I think it doesn't work. The code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <string>
Using namespace std;
Int main (){
 Fstream myfile;
 String name,line;
 Cin>>name;
 myfile.open(name.c_str(), ios::out);
 If (!myfile.is_open) return -1;
 Else {
   Cin>>line;
   myfile<<line;
   myfile.close();
 }
 Return 0;
}

It's good, compiler doesn't give me error, but program never opens file. I tried also to set a constant in function .open() ("name.txt") but it doesn't work. It doesn't give me -1, but it doesn't write (also open) file.
What kind of problem could it be?

P.s. I use CLion
You're missing the parens after myfile.is_open.
So instead of calling the function, you are testing the function pointer, which is true (non-zero).
And, strangely, you have a bunch of capitalized keywords.
They should be lowercase.

I might write it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main (){
    string name;
    cin >> name;

    ofstream myfile(name);
    if (!myfile)
        return -1;

    string line;
    cin >> line;
    myfile << line;

    return 0;
}
Last edited on
I wrote the code with smartphone, so that's why I have capitalized keywords, and in the code I don't miss the parens.
when you use a relative path/filename the file will be stored at the current path. This might not what you expect. Try an absolute path.
@coder777, He says that the open command didn't fail and yet, magically, the file didn't open either. Of course, typing a bunch of crap on your phone and then asking what's wrong with it is basically retarded, so....
Topic archived. No new replies allowed.