Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
lets say that there is a code.txt with the .cpp file

#include<iostream>
#include<string>
#include<fstream>

using namespace std;

int main(){
string file;
cout <<"enter file name";
cin  >> file;
system(file);
}

is there an a way to open a txt file by using the user input. i only know for example system("code.txt"); but what if if it is user-defined. please help.
Posted

1 solution

Try it.
If "code.txt" works, then if the user enters "code.txt" it will do the same thing.
If the user enters "D:\Temp\DeleteMyHardDiskPlease.exe" then the system will look for an executable of that name in that location and try to run it. So...I'd be careful what you let the user enter...
 
Share this answer
 
Comments
Frankie-C 22-May-15 13:02pm    
He can force the app to be used to open file so won't run something:
system("notepad.exe " + file);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900