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!