Re: [suse-programming-e] Directory Handling -- New to C++
<linuxdev@sptc.net> writes:
Hello, I am trying to write a program in C++ and I need to check to see if a directory exists, if not create it, move a file and verify that all works well. How do I go about this in Linux?
To check if a directory exists: extern "C" { #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> } #include <iostream> using namespace std; struct stat dir_stat; int main(int argc, char* argv[]) { stat(argv[1], &dir_stat); if (S_ISDIR(dir_stat.st_mode)) { cout << argv[1] << " is a directory" << endl; } else { cout << argv[1] << " is a NOT directory" << endl; } } markgray@soyo:/ARK/BOOKS/Efficient_c++> g++ -Wall dircheck.cpp markgray@soyo:/ARK/BOOKS/Efficient_c++> ./a.out /tmp /tmp is a directory markgray@soyo:/ARK/BOOKS/Efficient_c++> ./a.out dircheck.cpp dircheck.cpp is a NOT directory markgray@soyo:/ARK/BOOKS/Efficient_c++> ./a.out no_such_thing no_such_thing is a NOT directory markgray@soyo:/ARK/BOOKS/Efficient_c++> See man 2 stat. (You will be advised of course to check out the other macros described in the man page). To create a directory, see man 2 mkdir. To move a file, see man 2 rename. Hope this helps. [snip]
participants (1)
-
Mark Gray