Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to find whether a file already exists in a folder?

how to find whether a file already exists in a folder?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++tutorial
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Deepak Samuel
    wrote on last edited by
    #1

    Hi, This might sound a bit silly as this is not supposed to be a question in the VC++ forum. Nevertheless, I hope I can get an answer from somebody How to find whether a file already exists in a folder? Regards and thanks in advance Deepak Samuel

    Y J V 3 Replies Last reply
    0
    • D Deepak Samuel

      Hi, This might sound a bit silly as this is not supposed to be a question in the VC++ forum. Nevertheless, I hope I can get an answer from somebody How to find whether a file already exists in a folder? Regards and thanks in advance Deepak Samuel

      Y Offline
      Y Offline
      yndfcd
      wrote on last edited by
      #2

      WIN32_FIND_DATA wsd; HANDLE hFind = FindFirstFile(pszFileName, &wsd); if(hFind != NULL) { //the file does exist }else{ MessageBox(NULL, "Error", "The specified file does not exist.",MB_OK); } FindClose(hFind);

      1 Reply Last reply
      0
      • D Deepak Samuel

        Hi, This might sound a bit silly as this is not supposed to be a question in the VC++ forum. Nevertheless, I hope I can get an answer from somebody How to find whether a file already exists in a folder? Regards and thanks in advance Deepak Samuel

        J Offline
        J Offline
        Jeryth
        wrote on last edited by
        #3

        Open up "My Computer" and double-click the folders to the right directory... I take it you're asking about file existance from an fstream point of view. If you're looking if "foo.txt" exists you can do something like the following:

        ifstream fin( "foo.txt" );
        if ( fin )
        {
        //You get in here the file's already there
        }
        else
        {
        //File's not created
        }

        You could also use the ios flags ios::nocreate and ios::noreplace with an ofstream object:

        ofstream fout( "foo.txt" ios::noreplace )
        if ( fout )
        {
        //File didn't exist but it does now
        }
        else
        {
        //File existed and your fout creation failed
        }

        ||

        ofstream fout( "foo.txt" ios::nocreate )
        if ( fout )
        {
        //File existed but it's about to get truncated
        }
        else
        {
        //File doesn't exist and your fout creation failed
        }

        Hopefully that helped a little. Always Fear the Man with Nothing to Lose Jeryth

        1 Reply Last reply
        0
        • D Deepak Samuel

          Hi, This might sound a bit silly as this is not supposed to be a question in the VC++ forum. Nevertheless, I hope I can get an answer from somebody How to find whether a file already exists in a folder? Regards and thanks in advance Deepak Samuel

          V Offline
          V Offline
          vcplusplus
          wrote on last edited by
          #4

          From MSDN Library /* ACCESS.C: This example uses _access to check the * file named "ACCESS.C" to see if it exists and if * writing is allowed. */ #include #include #include void main( void ) { /* Check for existence */ if( (_access( "ACCESS.C", 0 )) != -1 ) { printf( "File ACCESS.C exists\n" ); /* Check for write permission */ if( (_access( "ACCESS.C", 2 )) != -1 ) printf( "File ACCESS.C has write permission\n" ); } } http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/\_crt\_\_access.2c\_.\_waccess.asp

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups