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 existence of file in C++

check existence of file in C++

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++perldatabase
12 Posts 8 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.
  • M Offline
    M Offline
    meixiang6
    wrote on last edited by
    #1

    #include #include #include #include #include // getcwd() definition #include // MAXPATHLEN definition using namespace std; int main() { string database; cout << "Select full path of Database" << endl; cin >> database; How do I check the existence of a file in C++? In perl it would be something like: if (!database){ print "No database found\n"; exit; } I tried looking online for hours but all the suggestions didnt work so far.. Thanks in advance!

    I D S D 4 Replies Last reply
    0
    • M meixiang6

      #include #include #include #include #include // getcwd() definition #include // MAXPATHLEN definition using namespace std; int main() { string database; cout << "Select full path of Database" << endl; cin >> database; How do I check the existence of a file in C++? In perl it would be something like: if (!database){ print "No database found\n"; exit; } I tried looking online for hours but all the suggestions didnt work so far.. Thanks in advance!

      I Offline
      I Offline
      Iain Clarke Warrior Programmer
      wrote on last edited by
      #2

      do a seatch for PathFileExists. Iain.

      Codeproject MVP for C++, I can't believe it's for my lounge posts...

      M U 2 Replies Last reply
      0
      • I Iain Clarke Warrior Programmer

        do a seatch for PathFileExists. Iain.

        Codeproject MVP for C++, I can't believe it's for my lounge posts...

        M Offline
        M Offline
        meixiang6
        wrote on last edited by
        #3

        PathFileExists is a windows API and I am running C++ in linux so it doesnt work as far as I know and have been trying unless I goofed up...

        E M D 3 Replies Last reply
        0
        • M meixiang6

          PathFileExists is a windows API and I am running C++ in linux so it doesnt work as far as I know and have been trying unless I goofed up...

          E Offline
          E Offline
          enhzflep
          wrote on last edited by
          #4

          I think you should be able to use _findfirst and _findnext on 'nix, I can't remember. They're in io.h Or you could try to open the file and look at the function result....

          1 Reply Last reply
          0
          • M meixiang6

            PathFileExists is a windows API and I am running C++ in linux so it doesnt work as far as I know and have been trying unless I goofed up...

            M Offline
            M Offline
            meixiang6
            wrote on last edited by
            #5

            After a lot of wrong examples from googling the web, I finally find something that works: myFileName = file; inp.open(myFileName.c_str(), ifstream::in); inp.close(); if(inp.fail()) { inp.clear(ios::failbit); cout << "No File found " << myFileName.c_str() << endl; exit(5); } else { cout << "Found File" << myFileName.c_str() << endl; } Thanks, hope this might help someone else... Bye the way is this board only for Visual C++ or can someone working on Linux with C++ ask Qs?

            R 1 Reply Last reply
            0
            • M meixiang6

              PathFileExists is a windows API and I am running C++ in linux so it doesnt work as far as I know and have been trying unless I goofed up...

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

              meixiang6 wrote:

              ...I am running C++ in linux...

              It helps to mention this little requirement up front.

              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              1 Reply Last reply
              0
              • M meixiang6

                #include #include #include #include #include // getcwd() definition #include // MAXPATHLEN definition using namespace std; int main() { string database; cout << "Select full path of Database" << endl; cin >> database; How do I check the existence of a file in C++? In perl it would be something like: if (!database){ print "No database found\n"; exit; } I tried looking online for hours but all the suggestions didnt work so far.. Thanks in advance!

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

                meixiang6 wrote:

                How do I check the existence of a file in C++?

                It's not C++, but you can use _access().

                "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                1 Reply Last reply
                0
                • M meixiang6

                  After a lot of wrong examples from googling the web, I finally find something that works: myFileName = file; inp.open(myFileName.c_str(), ifstream::in); inp.close(); if(inp.fail()) { inp.clear(ios::failbit); cout << "No File found " << myFileName.c_str() << endl; exit(5); } else { cout << "Found File" << myFileName.c_str() << endl; } Thanks, hope this might help someone else... Bye the way is this board only for Visual C++ or can someone working on Linux with C++ ask Qs?

                  R Offline
                  R Offline
                  Rajesh R Subramanian
                  wrote on last edited by
                  #8

                  meixiang6 wrote:

                  Bye the way is this board only for Visual C++ or can someone working on Linux with C++ ask Qs?

                  We pretty much assume Visual C++ here, mostly even MFC is assumed to be used. Just in case you hadn't noticed the name of this board, it is MFC/C++. You can ask C++ on Unix/Linux questions here, but you must let us know of it when posting your question.

                  It is a crappy thing, but it's life -^ Carlo Pallini

                  1 Reply Last reply
                  0
                  • M meixiang6

                    #include #include #include #include #include // getcwd() definition #include // MAXPATHLEN definition using namespace std; int main() { string database; cout << "Select full path of Database" << endl; cin >> database; How do I check the existence of a file in C++? In perl it would be something like: if (!database){ print "No database found\n"; exit; } I tried looking online for hours but all the suggestions didnt work so far.. Thanks in advance!

                    S Offline
                    S Offline
                    Stuart Dootson
                    wrote on last edited by
                    #9

                    The standard way of checking file existence on *nix (it works on Windows as well) is stat or fstat[^], I believe.

                    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                    M 1 Reply Last reply
                    0
                    • I Iain Clarke Warrior Programmer

                      do a seatch for PathFileExists. Iain.

                      Codeproject MVP for C++, I can't believe it's for my lounge posts...

                      U Offline
                      U Offline
                      uzziah0
                      wrote on last edited by
                      #10

                      or FindFirstFile()

                      1 Reply Last reply
                      0
                      • S Stuart Dootson

                        The standard way of checking file existence on *nix (it works on Windows as well) is stat or fstat[^], I believe.

                        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                        M Offline
                        M Offline
                        meixiang6
                        wrote on last edited by
                        #11

                        I will give it a try tommorrow...thanks!

                        1 Reply Last reply
                        0
                        • M meixiang6

                          #include #include #include #include #include // getcwd() definition #include // MAXPATHLEN definition using namespace std; int main() { string database; cout << "Select full path of Database" << endl; cin >> database; How do I check the existence of a file in C++? In perl it would be something like: if (!database){ print "No database found\n"; exit; } I tried looking online for hours but all the suggestions didnt work so far.. Thanks in advance!

                          D Offline
                          D Offline
                          Dan 0
                          wrote on last edited by
                          #12

                          You can also try in Windows,

                          if ( 0 == _access( database.c_str(), 0 ) ) {
                          cout<<"No database found.\n"
                          }

                          and the nix equivelant is

                          if ( 0 == access( database.c_str(), 0 ) ) {
                          cout<<"No database found.\n"
                          }

                          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