Solved

Checking for File Existance

Posted on 2007-11-27
4
417 Views
Last Modified: 2010-04-21
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;
fin.open("data.txt",ios::in);
if( fin.is_open() )
{
cout<<"file exists"<<endl;
flag=true;
}
fin.close();

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.
0
Comment
Question by:dan4good
  • 2
4 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 20358807
>>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.
0
 
LVL 30

Accepted Solution

by:
Axter earned 500 total points
ID: 20358816
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

0
 
LVL 86

Expert Comment

by:jkr
ID: 20358819
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;
}
0
 

Author Closing Comment

by:dan4good
ID: 31411206
Thanks , great response time as well.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What does std::atomic give me? 7 113
Getting IP address 8 71
Which Linux flavors will this run on? 6 76
c++ reading data from file into two dimensional array 3 96
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now