Checking for File Existance

I am not sure why this is so difficult for me. I am using Borland 4.5.2 (I am building a 16-bit app) , and I just want to check if a file exists and set a boolean that can be used later. I have found code snippets by 'Googling' but they don't work. Please incude any #include's that are required to make this work.
this doesn't work for me....

#include <string>
#include <iostream>
#include <fstream>
bool flag = false;
fstream fin;"data.txt",ios::in);
if( fin.is_open() )
cout<<"file exists"<<endl;

in the above code's case, it assumes a later version of the fstream library than comes packaged with the Borland 4.5.2 I am using.
Who is Participating?
AxterConnect With a Mentor Commented:
Here's example code:

#include <sys/stat.h>
bool FileExist(const char* FileName)
     struct stat my_stat;
     return (stat(FileName, &my_stat) == 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");
     return 0;

Open in new window

>>this doesn't work for me....
Don't use it.
You should not use a file open method to test for file existence.

It's inefficient, and does not work as well as other methods.

You should use stat method instead.
You could just use

#include <sys/stat.h>

bool file_exists(const char* pName)
  struct stat st;

  if (-1 == stat(pName,&st)) return false;

  return true;
dan4goodAuthor Commented:
Thanks , great response time as well.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.