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. quick file question...

quick file question...

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 6 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.
  • N Offline
    N Offline
    Nitron
    wrote on last edited by
    #1

    How do I check if a file exists or not? I have tried the following: CString f, msg; f = __argv[i]; ifstream infile(f); if(!infile) { msg.Format("Could not open: %s",f); AfxMessageBox(msg); } else AfxMessageBox("file opened"); But if the file doesn't exist, it just makes one!!! Any input is appreciated. Thanks! Nitron _________________________________________-- message sent on 100% recycled electrons.

    T R C S C 5 Replies Last reply
    0
    • N Nitron

      How do I check if a file exists or not? I have tried the following: CString f, msg; f = __argv[i]; ifstream infile(f); if(!infile) { msg.Format("Could not open: %s",f); AfxMessageBox(msg); } else AfxMessageBox("file opened"); But if the file doesn't exist, it just makes one!!! Any input is appreciated. Thanks! Nitron _________________________________________-- message sent on 100% recycled electrons.

      T Offline
      T Offline
      Tomasz Sowinski
      wrote on last edited by
      #2

      Check the code below if you don't mind using Win32 API:

      bool FileExists(LPCTSTR szFilename)
      {
      WIN32_FIND_DATA findFileData;
      HANDLE hFind = FindFirstFile(szFilename, &findFileData);
      if (hFind == INVALID_HANDLE_VALUE)
      {
      return false;
      }
      VERIFY(FindClose(hFind));
      return true;
      }

      Tomasz Sowinski -- http://www.shooltz.com

      What is "scratch" and why can everything be made from it?

      N 1 Reply Last reply
      0
      • N Nitron

        How do I check if a file exists or not? I have tried the following: CString f, msg; f = __argv[i]; ifstream infile(f); if(!infile) { msg.Format("Could not open: %s",f); AfxMessageBox(msg); } else AfxMessageBox("file opened"); But if the file doesn't exist, it just makes one!!! Any input is appreciated. Thanks! Nitron _________________________________________-- message sent on 100% recycled electrons.

        R Offline
        R Offline
        Roman Fadeyev
        wrote on last edited by
        #3

        I usually use next approach bool is_file_exist(const char * szFilePath) { DWORD Code=::GetFileAttributes(szFilePath); return (long(Code)!=-1) && (!((FILE_ATTRIBUTE_DIRECTORY) & Code)); }

        1 Reply Last reply
        0
        • T Tomasz Sowinski

          Check the code below if you don't mind using Win32 API:

          bool FileExists(LPCTSTR szFilename)
          {
          WIN32_FIND_DATA findFileData;
          HANDLE hFind = FindFirstFile(szFilename, &findFileData);
          if (hFind == INVALID_HANDLE_VALUE)
          {
          return false;
          }
          VERIFY(FindClose(hFind));
          return true;
          }

          Tomasz Sowinski -- http://www.shooltz.com

          What is "scratch" and why can everything be made from it?

          N Offline
          N Offline
          Nitron
          wrote on last edited by
          #4

          Cool! You guys rock!!! One day I'm gonna grow up to be a knowledgable programmer just like you! :rose::rose::rose: Nitron _________________________________________-- message sent on 100% recycled electrons.

          1 Reply Last reply
          0
          • N Nitron

            How do I check if a file exists or not? I have tried the following: CString f, msg; f = __argv[i]; ifstream infile(f); if(!infile) { msg.Format("Could not open: %s",f); AfxMessageBox(msg); } else AfxMessageBox("file opened"); But if the file doesn't exist, it just makes one!!! Any input is appreciated. Thanks! Nitron _________________________________________-- message sent on 100% recycled electrons.

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            As you're using MFC, I use this: CFileStatus status; if( CFile::GetStatus( "test.dat", status ) ) { // file exists } Christian We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum ) Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002

            1 Reply Last reply
            0
            • N Nitron

              How do I check if a file exists or not? I have tried the following: CString f, msg; f = __argv[i]; ifstream infile(f); if(!infile) { msg.Format("Could not open: %s",f); AfxMessageBox(msg); } else AfxMessageBox("file opened"); But if the file doesn't exist, it just makes one!!! Any input is appreciated. Thanks! Nitron _________________________________________-- message sent on 100% recycled electrons.

              S Offline
              S Offline
              Space Ace
              wrote on last edited by
              #6

              The problem is clear. Some solutions have already been mentioned but the easiest way to do it is ifstream inFile("text.dat", ios::nocreate);The default value is ios::in

              1 Reply Last reply
              0
              • N Nitron

                How do I check if a file exists or not? I have tried the following: CString f, msg; f = __argv[i]; ifstream infile(f); if(!infile) { msg.Format("Could not open: %s",f); AfxMessageBox(msg); } else AfxMessageBox("file opened"); But if the file doesn't exist, it just makes one!!! Any input is appreciated. Thanks! Nitron _________________________________________-- message sent on 100% recycled electrons.

                C Offline
                C Offline
                Chris Losinger
                wrote on last edited by
                #7

                here's yet another way:

                #include >io.h>
                ...
                ...
                bool bFileExists = (_access(inFile, 0)==0);


                Conservative: One who admires radicals centuries after they're dead. -- Leo C. Rosten

                image effects!

                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