File detection

This is a discussion on File detection within the C++ Programming forums, part of the General Programming Boards category; Is there a way to check file existence. I know it tells you how in the tutorials, but that way ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    4

    Question File detection

    Is there a way to check file existence. I know it tells you how in the tutorials, but that way doesn't work for me.
    And I am using Visual C++ .net

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    >>Is there a way to check file existence.

    Yes.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post your code that "doesn't work". Maybe you did something wrong. Anyways, one way to check is to simply open the file in read mode (with no-create), if it works, the file exists, if not, it doesn't
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    4
    #include <io.h>
    Code:
    if(access(argv[1], 00)) //access returns 0 if the file can be accessed 
    
      {		   //under the specified method (00)
    
        cout<<"File does not exist";  //because it checks file existence
    
        return 0;
    
      }
    that is code from the c tutorial at http://www.cprogramming.com/tutorial/lesson14.html which I adapted to be used in my program, which the compiler returned the error of access not being a valid function

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    try this .
    Code:
    if (!=infile) // if not equal to name of file (ifstream infile) 
    {
    cerr << "cant open";
    exit (-1);
    }
    // then your code follows
    This checks for errors when opening a file.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21