checking for file existance

This is a discussion on checking for file existance within the C++ Programming forums, part of the General Programming Boards category; How? Loops? writting a temporary batch file in the background to use "If not exist" and "if exist"? Can if ...

  1. #1
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    checking for file existance

    How?

    Loops? writting a temporary batch file in the background to use "If not exist" and "if exist"?

    Can if statements in C or C++ do the same thing as the following batch file....

    if not exist c:\windows\write.exe goto no
    if exist c:\windows\write.exe goto yup

    :no
    echo.
    echo. Wordpad does not exist !
    echo.
    pause
    goto done

    :yup
    echo.
    echo you have wordpad
    echo.
    pause
    goto done

    :done
    cls
    exit
    The world is waiting. I must leave you now.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    FILE *ptr;
    
    if ( ( ptr = fopen( "c:\\somedir\\file.ext", "r+" ) == NULL ) ) {
      printf( "File does not exist!" );
    }

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    that doesn't work

    #include <iostream.h>
    #include <stdio.h>
    #include <conio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <fstream.h>
    #include <io.h>

    I've tried all of these files.

    Also, "once" i get it to work, you can have an else statement right? A message if it exists, and a different message if the file does not exist.
    The world is waiting. I must leave you now.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    To see if a file exists using streams you declare the stream and supply the stream with the name/path for the file you wish to associate with it. IF you attempt to read a file and it doesn't exist the stream will send a fail message and return 0. If you attempt to write to a file that doesn't exist, the default is to create the file in the current directory, but you can change the default behaviour to not create a file if it doesn't exist already and to create the file in any directory desired if you so desire. To prevent creating a file that doesn't exist when using a ofstream use the nocreate ios flag.

    ifstream fin("test1.txt");
    //this means open a stream to read from the file test1.txt which exists in the current directory.

    //if test1.txt doesn't exist or can't be opened say so
    if(!fin)
    {
    cout << "error in opening file" << endl;
    }

    //using ofstream open test2.txt which resides in current directory
    //write to the end of test2.txt if it exists
    //if test2.txt doesn't exist, don't create a file by that name.
    ofstream fout("test2.txt", ios::app | ios::nocreate);

    //if unable to open test2.txt for whatever reason
    if(ifout)
    {
    cout << "unable to open file" << endl;
    }

    to use streams to read/write to files you should include the fstream.h or fstream file (depending on your compiler).

  5. #5
    Registered User Coder's Avatar
    Join Date
    Aug 2001
    Location
    Cairo, Egypt
    Posts
    128

    Lightbulb

    Try : http://www.cprogramming.com/tutorial/lesson14.html
    which uses a function called access that checks a file existence. However, I think this function isn't standard (i.e you may not find it)

    I knew this topic was handled in one of the tutorials, but I actually got it using a google.com search (I was too blind to see the tutorial descriptions correctly)
    Muhammad Haggag

  6. #6
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    It's all solved

    It was asnwered on the C board with a different approach. Thanks !
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  3. checking existance
    By LloydUzari in forum C++ Programming
    Replies: 15
    Last Post: 12-23-2004, 09:36 PM
  4. Checking File Existance
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2002, 01:02 AM
  5. file existance checking /w code
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 02-08-2002, 01:40 PM

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