Help with Files in c++

Hello guys!

I have a few questions, wish you could help me:

1. I want the user to type in the name of the file as he/she wants to save it.
1
2
3
4
5
6
7
8
9
10
11
12
13
ofstream myFile;
             cout <<"Save the file as:" <<endl;
             string p;
             cin >> p;
             p=p+".txt";
             myFile.open(p);
             if(!myFile.is_open){
                cout<<" Error opening the file"<<endl;             
             }
             else{
                //Here goes code...
             }
             myFile.close();

But I am getting an error in this line:
myFile.open(p);

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

How can I solve this?


2. Let's say I want to open an unknown txt file (the user says how it's named).
So i thought that I would do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
              ifstream myFile;
              cout << "Type in the name of the txt file you want to open"<<endl;
              getline(cin,p);
              p=p+".txt";
              myFile.open(p);
              if(!myFile.is_open()){
                 cout<<" Error opening the file"<<endl;             
              }
              else{
              //Here goes code

              }
               myFile.close();


But I am getting the same error as above!
And the last question:

3. If I have in a file named list.txt the following:

56,34,23

And I want to show via cin the number: 56, then 34, then 23, I understand that I would user getline() in a while loop, but I quiet don't understand how to use it, so I would appreciate an example.


Thanks for anything in advance!



Last edited on
You need a string function called c_str()

 
myFile.open(p.c_str());


Oh, thanks!! And could you tell me what exactly that function does?
Last edited on
Firstly, I think you have two problems with the first code. You need if(!myFile.is_open())

and

myFile.open(p.c_str());

The reason for that is because C++ has some weird history with the C language and sometimes the string isn't read properly. I am guessing that is what is happening and why you got the error message.

As for your third question, <getline(myFile, line1);> would be the way to do it. I made an example of your program using the getline function:

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

using namespace std;

int main()
{
    
    ifstream myFile;
    string p;
    cout <<"Open File: " << endl;
    
    cin >> p;
    myFile.open(p.c_str());
    while (!myFile.is_open()){
        cout << "Error opening file" << endl;
        cout << "Please try again. " << endl;
        cout << "Open File: " << endl;
        cin >> p;
        
        myFile.open(p.c_str());
    }
    getline(myFile, p);
    
    cout << "The numbers are " << p << endl;
    myFile.close();
}


As you see, I did not put it in the while loop, I have it off to the side. getline() reads the whole line of characters until it hits a new line character. If you need anymore help, then don't be afraid to message me or post here obviously :).

Last edited on
Oh, thank you both for helping me out! awesome! And audioace, thanks, really!!
Note that the c_str() is not needed when calling fstream::open() in C++11, as they've added a new overload of the method.

So when compiling using g++ with the -std=c++11 switch or using VC++2010 or newer, the c_str() is not required.

1
2
3
4
5
6
    ifstream myFile;
    string p;

    // etc

    myFile.open(p); // ok in C++11 


Andy

From http://www.cplusplus.com/reference/fstream/fstream/open/

[C++11]

1
2
3
4
void open (const char* filename,
           ios_base::openmode mode = ios_base::in | ios_base::out);
void open (const string& filename,
           ios_base::openmode mode = ios_base::in | ios_base::out);

Last edited on
Note that the c_str() is not needed when calling fstream::open() in C++11, as they've added a new overload of the method.

So when compiling using g++ with the -std=C++11 switch or using VC++2010 or newer, the c_str() is not required.


Thanks for bringing that up, wasn't aware. Seems like more and more people are starting to use C++ 11, guess I need to move on eventually ;)
Topic archived. No new replies allowed.