hwo to check if a file exist in certain dir?

This is a discussion on hwo to check if a file exist in certain dir? within the C++ Programming forums, part of the General Programming Boards category; what is the syntax for that? if (xxx.txt exist) { ...... } thank you...

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

    hwo to check if a file exist in certain dir?

    what is the syntax for that?

    if (xxx.txt exist)
    {
    ......
    }

    thank you

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    Code:
    #include<fstream.h>  // include on top of program
    
    
           ifstream fin("C:\\ONEDIR\\SECOND\\FILE.TXT");
     
           if(fin.fail())
               cout  << "Can't open the file or it doesn't exist!";

    that's one way of doing so, i'm taking for granted that you know a bit about file streams b/c i'm not including any comments here
    and using just lines of instructional code without it's proper placement representation withing a function..

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    oh yeah,,,,,,,,,, and if it does exist then it will get opened so you must close it after, that is if you don't intend on using it



    Code:
    fin.close();

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    Code:
    ifstream X;
    
    X.open ("filename");
    
    if (!X)
    {
    	//do whatever
    }

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    Thank you very much!!

    thanks..... i will try that out!!

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, 12:44 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. writing/reading from file produces double results.
    By stumon in forum C Programming
    Replies: 4
    Last Post: 03-20-2003, 03:01 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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