Important alert: (current site time 7/15/2013 4:53:00 PM EDT)
 

VB icon

[\File Exist Algorithms/]

Email
Submitted on: 11/14/2002 11:00:10 PM
By: Josh Nixon  
Level: Beginner
User Rating: By 24 Users
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 45525
author picture
(About the author)
 
     This function will check to see if a file exist in the computer if it doesnt it will return -1 else 0.
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :[\File Exist Algorithms/]
//**************************************
#include <fstream.h>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: [\File Exist Algorithms/]
// Description:This function will check to see if a file exist in the computer if it doesnt it will return -1 else 0.
// By: Josh Nixon
//
// Assumes:This function will check to see if a file exist in the computer if it doesnt it will return -1 else 0.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=5186&lngWId=3//for details.//**************************************

int Check_File(char *FileName)
{
 ifstream FileL;
FileL.open(FileName);
if(!FileL)
{
 FileL.close();
 return -1;
}
else
{
 FileL.close();
 return 0;
}
}


Other 35 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

11/14/2002 11:01:02 PMJosh Nixon

its simple like pie folks vote or comments please :)
(If this comment was disrespectful, please report it.)

 
11/19/2002 12:24:57 PMHannibal Maz

This is neat for DOS programming, but the API ::DoesFileExist works for windoze easier.
(If this comment was disrespectful, please report it.)

 
1/21/2003 12:34:02 AM

In my head, it's easier to understand 1 means true, and 0 means false. May I ask why the return values don't follow those rules?
(If this comment was disrespectful, please report it.)

 
1/21/2003 7:11:29 AM

simple and clean, i like that. i may change what it returns, but otherwise excellent job.
(If this comment was disrespectful, please report it.)

 
3/20/2003 3:17:20 AM

I think this example is OK, we can checke it with FindFirstFile API call to find the file in the drive.
(If this comment was disrespectful, please report it.)

 
4/23/2003 3:20:16 AMMork2929

I think that the above 0 and 1 comment is exactly what I thought. In CS 1 is true 0 is false. It's how the world works.
(If this comment was disrespectful, please report it.)

 
6/6/2003 1:47:23 AM

Has anyone checked what happens if there is a sharing violation? I personally prefer to use FindFirstFile(). It's also faster.
(If this comment was disrespectful, please report it.)

 
7/15/2003 7:53:01 AM

This code is nice and works fine
I think it is fast working code for file search.
(If this comment was disrespectful, please report it.)

 
8/11/2003 12:25:46 AM

thanks for your code
(If this comment was disrespectful, please report it.)

 
9/12/2003 9:36:29 PMDavid Maisonave (Axter)

While this method may be portable, it's not very efficient, and will fail if the file is locked, or if the file is a write only file.

This method fails if the filename is a directory.
The following is a more efficient method that will work even if the file is lock, and it's portable in all major operating systems.
#include < sys/stat.h>
#include < io.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);
}

(If this comment was disrespectful, please report it.)

 
12/23/2003 9:36:12 AMOtets

FindFirstFile() is better
(If this comment was disrespectful, please report it.)

 
4/8/2004 5:46:15 PMCameron H.

Used KIS. (keep it simple)
Nice!
(If this comment was disrespectful, please report it.)

 
12/24/2004 9:47:08 AM

thank for this code
nice code
(If this comment was disrespectful, please report it.)

 
12/24/2004 9:48:35 AM

thank for program
(If this comment was disrespectful, please report it.)

 
1/29/2005 1:19:46 AM

Compatibility:C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++

(If this comment was disrespectful, please report it.)

 
5/8/2005 6:30:01 AM

thank for this code

(If this comment was disrespectful, please report it.)

 
6/1/2005 5:49:29 AMZac Sims

you don't need to close the connection to the file if it was never made...
(If this comment was disrespectful, please report it.)

 
2/9/2006 2:08:58 AMRinjani

Its so cool !!
(If this comment was disrespectful, please report it.)

 
7/7/2006 9:38:09 PMtin

can you do me a program file related to OS using C language? thanks
(If this comment was disrespectful, please report it.)

 
8/27/2006 1:22:17 PM

There are better and more efficient methods - see other notes
(If this comment was disrespectful, please report it.)

 
11/9/2006 4:33:45 AMArman

HI
(If this comment was disrespectful, please report it.)

 
11/11/2006 10:49:37 AMmanish

this is a marblase code that i even seen
(If this comment was disrespectful, please report it.)

 
11/21/2006 8:00:49 AMcentrobiom

you can learn more of the file prog
manipulation check the doc of art of c++ programming
(If this comment was disrespectful, please report it.)

 
11/25/2006 5:57:06 PMOle Rasmussen

This is C++, and NOT C compatible.
(If this comment was disrespectful, please report it.)

 
2/4/2007 12:14:21 AMSergiu

I don't know what happened ,but it keeps telling me that it found the file ,whatever I type.
(If this comment was disrespectful, please report it.)

 
12/14/2007 8:28:31 AMDeeps

Its fine and smooth in dos
(If this comment was disrespectful, please report it.)

 
11/6/2008 4:37:41 AMEB

Tried on vista it works but it's only searching in the folder in which the prog is running.
(If this comment was disrespectful, please report it.)

 
8/17/2009 11:41:52 AMDiego

Ok, it's simple. :)
Good lucky.
(If this comment was disrespectful, please report it.)

 
8/2/2010 12:51:22 AMavi

pls sir how i can use or add the header file which r in not alredy in library.u r most of code that require header file i could not add it.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.