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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Does file exist?

Does file exist?

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

    Is there a way to check if a file exist?? I'm using ::GetPrivateProfileXXX(...) funktion and if the file doesn't exist my app crashes... so I need a way to check if my INI exist! ------------------------------------ Rickard Andersson, Suza Computing ICQ#: 50302279 I'm from the winter country SWEDEN! ------------------------------------

    M J J M 4 Replies Last reply
    0
    • R Rickard Andersson20

      Is there a way to check if a file exist?? I'm using ::GetPrivateProfileXXX(...) funktion and if the file doesn't exist my app crashes... so I need a way to check if my INI exist! ------------------------------------ Rickard Andersson, Suza Computing ICQ#: 50302279 I'm from the winter country SWEDEN! ------------------------------------

      M Offline
      M Offline
      Mike Nordell
      wrote on last edited by
      #2

      GetFileAttributes

      1 Reply Last reply
      0
      • R Rickard Andersson20

        Is there a way to check if a file exist?? I'm using ::GetPrivateProfileXXX(...) funktion and if the file doesn't exist my app crashes... so I need a way to check if my INI exist! ------------------------------------ Rickard Andersson, Suza Computing ICQ#: 50302279 I'm from the winter country SWEDEN! ------------------------------------

        J Offline
        J Offline
        Jack Handy
        wrote on last edited by
        #3

        If you are using MFC you can use CFile like so..

        if (f.Open(filename, CFile::modeRead) == TRUE) {

        Otherwise you could do something similar with fopen() and check for NULL -Jack To an optimist the glass is half full. To a pessimist the glass is half empty. To a programmer the glass is twice as big as it needs to be.

        1 Reply Last reply
        0
        • R Rickard Andersson20

          Is there a way to check if a file exist?? I'm using ::GetPrivateProfileXXX(...) funktion and if the file doesn't exist my app crashes... so I need a way to check if my INI exist! ------------------------------------ Rickard Andersson, Suza Computing ICQ#: 50302279 I'm from the winter country SWEDEN! ------------------------------------

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          #include <io.h>
          ...
          if(_access(filename,0)==0){
          // file exists
          }

          Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          L 1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            #include <io.h>
            ...
            if(_access(filename,0)==0){
            // file exists
            }

            Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            FILE *fp = fopen("C:\\file.txt", "r"); if (!fp) } //... no file { else { //... there is a file fclose(fp); }

            1 Reply Last reply
            0
            • R Rickard Andersson20

              Is there a way to check if a file exist?? I'm using ::GetPrivateProfileXXX(...) funktion and if the file doesn't exist my app crashes... so I need a way to check if my INI exist! ------------------------------------ Rickard Andersson, Suza Computing ICQ#: 50302279 I'm from the winter country SWEDEN! ------------------------------------

              M Offline
              M Offline
              mynab
              wrote on last edited by
              #6

              Also for information there is a simple way to check if a file is writable or not:

              FILE *f = fopen(filename, "ab");
              if (!f)
              {
              // file is not writable
              }
              else
              {
              fclose(f);
              }

              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