• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

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
0
Fordster55
Asked:
Fordster55
3 Solutions
 
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) ;

}
0
 
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!
0
 
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;
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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.




0
 
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
0
 
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.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now