Reputation: 149
I am trying to open a text file by getting user input.
system(topicName.c_str());
is not working where topicName is the user input.
Even as a user I am inputting the right name of the file it is not opening with the corresponding file (.txt)
cout << "Intro To C " <<endl;
cout << "Intro To C++" <<endl;
cout << "Intro To Java " <<endl;
cout << "\t\tWhich Topic You Want to edit: ";
cin.ignore(1000, '\n');
getline(cin, topicName);
system(topicName.c_str());
Upvotes: 0
Views: 11135
Reputation: 2408
Suppose you have the following files in the directory in which you are running the program:
Then, the following code will serve your job:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
cout << "Intro To C" << endl;
cout << "Intro To C++" << endl;
cout << "Intro To Java" << endl;
string topicName;
cout << "\t\tWhich Topic You Want to edit: ";
getline(cin, topicName);
topicName = "notepad \"" + topicName + "\"";
system(topicName.c_str());
return 0;
}
Then upon running, we get the following output:
Intro To C
Intro To C++
Intro To Java
Which Topic You Want to edit: Intro To Java
Upon typing this, the file will be opened in notepad if you are running it in a Windows system. Please change your program accordingly if you are using a different operating system.
Upvotes: 7