Checking File Existance

This is a discussion on Checking File Existance within the C++ Programming forums, part of the General Programming Boards category; Hey everyone... Is there any way to check if a file exists?...

  1. #1
    Unregistered
    Guest

    Checking File Existance

    Hey everyone...

    Is there any way to check if a file exists?

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Check for existence when you try to open it perhaps? As in the following code, if opening returns NULL, it ain't where you're lookin for it, or can't be opened for some other reason.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       FILE *in
    
       if ((in = fopen("\\AUTOEXEC.BAT", "r"))
           == NULL)
       {
          printf("Cannot open input file.\n");
          return 1;
       }
    return 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    8
    Here's one way of doing it:
    Code:
    #include <stdio.h>
    
    int main ()
    {
    FILE *fp;
    if ((fp=fopen("c:\\test.bin", "r")) == NULL)
    {
    	printf("Blahhh");
    }
      return 0;
    }
    Last edited by jizzer; 04-25-2002 at 02:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM

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