i need to write a program that will check if a key.program_name file exists in a directory, if so run it if not open a web page

i need to write a program that will check if a key.program_name file exists in a directory, if so run it if not open a web page
LVL 1
Fordster55Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

B1-66ERCommented:
Hi Fordster55.
You can use standart fstream functions, to check if file exist:

#include <iostream>
#include <fstream>

using namespace std ;

int
main(int argc, char*argv[])
{

  ifstream f("/home/lynx/tmp") ;
  if (!f)
   cout << "cant open" << endl ;
  while(1) ;

}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AxterCommented:
Hi Fordster55,
I recommend that you use the stat method or access method, which are more reliable than trying to open a file and read it.
These methods are also much faster.
Example code for stat method:

#include <sys/stat.h>

bool FileExist(const char* FileName)
{
     struct stat my_stat;
     return (stat(FileName, &my_stat) == 0);
}

bool IsDirectory(const char* FileName)
{
     struct stat my_stat;
     if (stat(FileName, &my_stat) != 0) return false;
     return ((my_stat.st_mode & S_IFDIR) != 0);
}

int main(int argc, char* argv[])
{
     bool v1 = FileExist("c:\\autoexec.bat");
     bool v2 = FileExist("c:\\nofile.bat");
     bool v3 = FileExist("c:\\config.sys");
     bool v4 = FileExist("c:\\nofile2.bat");
     bool v5 = IsDirectory("c:\\windows");
     bool v6 = IsDirectory("c:\\notA_dir");
     bool v7 = IsDirectory("c:\\WINNT");
     return 0;
}


David Maisonave (Axter)
Cheers!
AxterCommented:
The stat method is more portable than the access method, but the access method can be more reliable when dealing with mapped drives and redirect paths.
Example:
#include <sys/stat.h>

bool FileExist(const char* FileName)
{
     struct stat my_stat;
     return (stat(FileName, &my_stat) == 0);
}

bool IsDirectory(const char* FileName)
{
     struct stat my_stat;
     if (stat(FileName, &my_stat) != 0) return false;
     return ((my_stat.st_mode & S_IFDIR) != 0);
}

int main(int argc, char* argv[])
{
     bool v1 = FileExist("c:\\autoexec.bat");
     bool v2 = FileExist("c:\\nofile.bat");
     bool v3 = FileExist("c:\\config.sys");
     bool v4 = FileExist("c:\\nofile2.bat");
     bool v5 = IsDirectory("c:\\windows");
     bool v6 = IsDirectory("c:\\notA_dir");
     bool v7 = IsDirectory("c:\\WINNT");
     return 0;
}
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

grg99Commented:
What do you really want to know?  

(1)  Is the file there.

(2)  Is the file readable by you.

(3)  Is the file readable as input to a particular program running as a particular login user.

(4)  Is the file executable by you or some arbitrary user.


All very different questions.

In the end they allare insufficient, as they're inquiries as to the past status of the file.  Any second after that, the file could be deleted,
write_locked, truncated, renamed, permissions changed, ACL list added, almost anything.

The only way to really know is to try "running" it, whatever that means, and check the error code.




Raj_KauCommented:
Hi
to check file exist or not you use "FindFirstFile" API

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/findfirstfile.asp

if this return error then you can open the webpage through ShellExecute

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shellexecute.asp


i think this help you


Raj
AxterCommented:
>>to check file exist or not you use "FindFirstFile" API

Using findfile method is not as efficient as using the stat method or access method.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.