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. Check if file is there or not [modified]

Check if file is there or not [modified]

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

    I curently have this code bool FilesClass::FilePath(char name[50]) { int buffer; int fh; if( _sopen_s( &fh, name, _O_BINARY, _SH_DENYWR, 0 ) ) { Files++; buffer=strlen(name); strncat(DllStatus,name,buffer); strncat(DllStatus,";",2); return false; } else return true; } But in some cases it fails to process the checks, and asserts with cant open file. My idea is that this function either returns true if the file is present, or false if its not, any sugestions? -- modified at 17:45 Tuesday 5th December, 2006

    M D H T 4 Replies Last reply
    0
    • F FredrickNorge

      I curently have this code bool FilesClass::FilePath(char name[50]) { int buffer; int fh; if( _sopen_s( &fh, name, _O_BINARY, _SH_DENYWR, 0 ) ) { Files++; buffer=strlen(name); strncat(DllStatus,name,buffer); strncat(DllStatus,";",2); return false; } else return true; } But in some cases it fails to process the checks, and asserts with cant open file. My idea is that this function either returns true if the file is present, or false if its not, any sugestions? -- modified at 17:45 Tuesday 5th December, 2006

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      What do you mean fails to process the checks? How about something like this:

      bool FilesClass::FilePath(const char *name)
      {
      int buffer;
      int fh;

      errno\_t errno = \_sopen\_s( &fh, name, \_O\_BINARY | \_O\_RDONLY, \_SH\_DENYWR, \_S\_IREAD );
      
      if( errno )
      { 
      	if (errno == ENOENT)
      	{
      		Files++;
      
      		buffer=strlen(name);
      		strncat(DllStatus,name,buffer);
      		strncat(DllStatus,";",2);
      	}
      	else
      	{
      		// \_sopen\_s() failed!
      	}
      
      	return false;
      }
      else
      {
      	\_close( fh );
      	return true;
      }
      

      }

      The pmode param is required in the call to _sopen_s(). Note that if the file is already opened elsewhere without read share access FilePath() will return false.

      F 1 Reply Last reply
      0
      • M Mark Salsbery

        What do you mean fails to process the checks? How about something like this:

        bool FilesClass::FilePath(const char *name)
        {
        int buffer;
        int fh;

        errno\_t errno = \_sopen\_s( &fh, name, \_O\_BINARY | \_O\_RDONLY, \_SH\_DENYWR, \_S\_IREAD );
        
        if( errno )
        { 
        	if (errno == ENOENT)
        	{
        		Files++;
        
        		buffer=strlen(name);
        		strncat(DllStatus,name,buffer);
        		strncat(DllStatus,";",2);
        	}
        	else
        	{
        		// \_sopen\_s() failed!
        	}
        
        	return false;
        }
        else
        {
        	\_close( fh );
        	return true;
        }
        

        }

        The pmode param is required in the call to _sopen_s(). Note that if the file is already opened elsewhere without read share access FilePath() will return false.

        F Offline
        F Offline
        FredrickNorge
        wrote on last edited by
        #3

        Ok, found my assert problem as well, i actualy check for total of 6222 files, array was to short. Thanks, your code worked better. -- modified at 18:26 Tuesday 5th December, 2006

        1 Reply Last reply
        0
        • F FredrickNorge

          I curently have this code bool FilesClass::FilePath(char name[50]) { int buffer; int fh; if( _sopen_s( &fh, name, _O_BINARY, _SH_DENYWR, 0 ) ) { Files++; buffer=strlen(name); strncat(DllStatus,name,buffer); strncat(DllStatus,";",2); return false; } else return true; } But in some cases it fails to process the checks, and asserts with cant open file. My idea is that this function either returns true if the file is present, or false if its not, any sugestions? -- modified at 17:45 Tuesday 5th December, 2006

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          FredrickNorge wrote:

          My idea is that this function either returns true if the file is present, or false if its not, any sugestions?

          _access(..., 0) sounds a whole lot simpler.


          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

          "Judge not by the eye but by the heart." - Native American Proverb

          1 Reply Last reply
          0
          • F FredrickNorge

            I curently have this code bool FilesClass::FilePath(char name[50]) { int buffer; int fh; if( _sopen_s( &fh, name, _O_BINARY, _SH_DENYWR, 0 ) ) { Files++; buffer=strlen(name); strncat(DllStatus,name,buffer); strncat(DllStatus,";",2); return false; } else return true; } But in some cases it fails to process the checks, and asserts with cant open file. My idea is that this function either returns true if the file is present, or false if its not, any sugestions? -- modified at 17:45 Tuesday 5th December, 2006

            H Offline
            H Offline
            Hamid Taebi
            wrote on last edited by
            #5

            In additinal yo can use of FindFirstFile


            WhiteSky


            1 Reply Last reply
            0
            • F FredrickNorge

              I curently have this code bool FilesClass::FilePath(char name[50]) { int buffer; int fh; if( _sopen_s( &fh, name, _O_BINARY, _SH_DENYWR, 0 ) ) { Files++; buffer=strlen(name); strncat(DllStatus,name,buffer); strncat(DllStatus,";",2); return false; } else return true; } But in some cases it fails to process the checks, and asserts with cant open file. My idea is that this function either returns true if the file is present, or false if its not, any sugestions? -- modified at 17:45 Tuesday 5th December, 2006

              T Offline
              T Offline
              ThatsAlok
              wrote on last edited by
              #6

              FredrickNorge wrote: My idea is that this function either returns true if the file is present, or false if its not, any sugestions? try PathFileExists

              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and you

              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