Меню Рубрики

C file exists linux

C++ – Determining if directory (not a file) exists in Linux [duplicate]

How would I determine if a directory (not a file) existed using C++ in Linux? I tried using the stat() function but it returned positive when a file was found. I only want to find if the inputted string is a directory, not something else.

6 Answers 6

According to man(2) stat you can use the S_ISDIR macro on the st_mode field:

Side note, I would recommend using Boost and/or Qt4 to make cross-platform support easier if your software can be viable on other OSs.

how about something i found here

If you can check out the boost filesystem library. It’s a great way to deal with this kind of problems in a generic and portable manner.

In this case it would suffice to use:

The way I understand your question is this: you have a path, say, /foo/bar/baz (baz is a file) and you want to know whether /foo/bar exists. If so, the solution looks something like this (untested):

In C++17**, std::filesystem provides two variants to determine the existence of a path:

  1. is_directory() determines, if a path is a directory and does exist in the actual filesystem
  2. exists() just determines, if the path exists in the actual filesystem (not checking, if it is a directory)

Example (without error handling):

Both functions throw filesystem_error in case of errors. If you want to avoid catching exceptions, use the overloaded variants with std::error_code as second parameter.

If you want to find out whether a directory exists because you want to do something with it if it does (create a file/directory inside, scan its contents, etc) you should just go ahead and do whatever you want to do, then check whether it failed, and if so, report strerror(errno) to the user. This is a general principle of programming under Unix: don’t try to figure out whether the thing you want to do will work. Attempt it, then see if it failed.

If you want to behave specially if whatever-it-was failed because a directory didn’t exist (for instance, if you want to create a file and all necessary containing directories) you check for errno == ENOENT after open fails.

I see that one responder has recommended the use of boost::filesystem . I would like to endorse this recommendation, but sadly I cannot, because boost::filesystem is not header-only, and all of Boost’s non-header-only modules have a horrible track record of causing mysterious breakage if you upgrade the shared library without recompiling the app, or even if you just didn’t manage to compile your app with exactly the same flags used to compile the shared library. The maintenance grief is just not worth it.

Читайте также:  Как сделать загрузочный диск kali linux

Источник

C file exists linux

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

Are you new to LinuxQuestions.org? Visit the following links:
Site Howto | Site FAQ | Sitemap | Register Now

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux – A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter. For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author’s experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Источник

How to check if a file exists and is readable in C++?

I’ve got a fstream my_file(“test.txt”), but I don’t know if test.txt exists. In case it exists, I would like to know if I can read it, too. How to do that?

8 Answers 8

I would probably go with:

The good method checks if the stream is ready to be read from.

You might use Boost.Filesystem. It has a boost::filesystem::exist function.

I don’t know how about checking read access rights. You could look in Boost.Filesystem too. However likely there will be no other (portable) way than try to actually read the file.

What Operating System/platform?

On Linux/Unix/MacOSX, you can use fstat.

On Windows, you can use GetFileAttributes.

Usually, there is no portable way of doing this with standard C/C++ IO functions.

if you are on unix then access() can tell you if it’s readable. However if ACL’s are in use, then it gets more complicated, in this case it’s best to just open the file with ifstream and try read.. if you cannot read then the ACL may prohibit reading.

Since C++11 it’s possible to use implicit operator bool instead of good() :

Consider also checking for the file type.

I know the poster eventually said they were using Linux, but I’m kind of surprised that no one mentioned the PathFileExists() API call for Windows.

You will need to include the Shlwapi.lib library, and Shlwapi.h header file.

the function returns a BOOL value and can be called like so:

Concerning the use of fstat in windows, I am not sure if it is what you want. From Microsoft the file must be already open. Stat should work for you.

Источник

Linux and Csharp, Check If File/Folder Doesn’t Exist in Linux, if so, run MKDIR via Csharp SSH – [closed]

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 4 years ago .

Using SSH.Net I would like to do the following: Make a command to check if the file exists on a Linux Machine via C# and various ssh packages and/or winscp libraries and if the file or files do not exist on the linux machine, then it will create them, I enjoy the educational aspects of programming, so I am improving ones self, I apologise for not fully writing out the question.

2 Answers 2

This is something I ran into not long ago so trust me on here.

Let’s check out this little guy below:

In a way you’re already answering your question, just not comprehending the syntax. (check it out)

Before even getting into a third-party assembly or library/whatever, I think you need to first create a simple console application to find a file on your machine.

Create the folder I just described in-code and “DoWork();”

You can do mkdir -p /var/www/html/app/cs to create the directory and its parent if it doesn’t exist.

The command will succeed as long as you’ve got permissions to create the first directory that doesn’t exist. If only /var/www exists, it will create html , then app , then cs . If /var/www/html/app/cs exists, it will do nothing.

So your code would just be:

Note that you should check errors after nearly each step: SSH connection can fail, command can fail because of permissions, etc .

Источник

Fastest way to check if a file exist using standard C++/C++11/C?

I would like to find the fastest way to check if a file exist in standard C++11, C++, or C. I have thousands of files and before doing something on them I need to check if all of them exist. What can I write instead of /* SOMETHING */ in the following function?

19 Answers 19

Well I threw together a test program that ran each of these methods 100,000 times, half on files that existed and half on files that didn’t.

Results for total time to run the 100,000 calls averaged over 5 runs,

The stat() function provided the best performance on my system (Linux, compiled with g++ ), with a standard fopen call being your best bet if you for some reason refuse to use POSIX functions.

Remark : in C++14 and as soon as the filesystem TS will be finished and adopted, the solution will be to use:

and since C++17, only:

I use this piece of code, it works OK with me so far. This does not use many fancy features of C++:

For those who like boost:

It depends on where the files reside. For instance, if they are all supposed to be in the same directory, you can read all the directory entries into a hash table and then check all the names against the hash table. This might be faster on some systems than checking each file individually. The fastest way to check each file individually depends on your system . if you’re writing ANSI C, the fastest way is fopen because it’s the only way (a file might exist but not be openable, but you probably really want openable if you need to “do something on it”). C++, POSIX, Windows all offer additional options.

While I’m at it, let me point out some problems with your question. You say that you want the fastest way, and that you have thousands of files, but then you ask for the code for a function to test a single file (and that function is only valid in C++, not C). This contradicts your requirements by making an assumption about the solution . a case of the XY problem. You also say “in standard c++11(or)c++(or)c” . which are all different, and this also is inconsistent with your requirement for speed . the fastest solution would involve tailoring the code to the target system. The inconsistency in the question is highlighted by the fact that you accepted an answer that gives solutions that are system-dependent and are not standard C or C++.

Without using other libraries, I like to use the following code snippet:

This works cross-platform for Windows and POSIX-compliant systems.

Источник

Adblock
detector