I'm still learning c++(hence the beginner forum) so please don't laugh at my code.
I'm basically making a program to copy a file, from user inputted file names, which actually works as posted below. However, I want to know if there's a way so the user (on windows system) can input the file name and my program will find it (the file to read from) EVEN IF IT'S IN SOME OTHER FOLDER OF THE C:\ DRIVE AND NOT IN THE SAME FOLDER AS MY PROJECT?
I'm aware my error checking is not great and plan on improving it, just wanted to get the files opening like I want and copying correctly (which it does do now)
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int fileCopy(); //gets filenames and copies input file to user designated output file, returning -1 if unsuccessful
int main()
{
int x;
int check; //holds return value of fileCopy function to check if successful with file copying
check=fileCopy();
if(check<0)
{
cout<<"Sorry, the input or output file could not be opened. Please run again!"<<endl;
}
elseif(check>0)
{
cout<<"You'r output file has been created successfully!";
}
cin>>x;
return 0;
}
int fileCopy()
{
char c;
string infileName;
string outfileName;
ifstream infile;
ofstream outfile;
cout<<"Please enter the name of the file you would like to copy: ";
cin>>infileName;
cout<<endl<<"Please enter the name of the file you would like to copy "<<infileName<<" to: ";
cin>>outfileName;
infile.open(infileName);
outfile.open(outfileName);
if((!infile.is_open()) || (!outfile.is_open()))
{
return -1;
}
if((infile.is_open()) && (outfile.is_open()))
{
do
{
infile.get(c);
outfile.put(c);
}while(!infile.eof());
infile.close();
outfile.close();
return 1;
}
}
and the cin>>x; at the end is the only way i can get the console window to stay open so i can see what the output of the program is.
A simple answer, no. Unless you know specifically what absolute folder it is in (starting with a drive letter), or what folder it is in relative to your executable, there is no easy way to do this quickly.
If you really needed to, you could search the entire drive to find the file, but even then you don't know if it is the correct file and it can take quite some time (I'm sure you've tried to search your drive before in Windows Explorer; it takes time.)