What is the difference between remove and unlink functions in C++?
Apart from the fact that unlink is unix-specific (as pointed out by Chris), we read in the POSIX manual:
If path does not name a directory, remove(path) is equivalent to unlink(path).
If path names a directory, remove(path) is equivalent to rmdir(path).
As for the directory-passed unlink, we read:
The path argument must not name a directory unless the process has appropriate privileges and the implementation supports using unlink() on directories. (...) Applications should use rmdir() to remove a directory.
remove is portable, and unlink is Unix-specific. :-P
The remove() function removes the file or directory specified by path.
If path specifies a directory, remove(path) is the equivalent of
rmdir(path). Otherwise, it is the equivalent of unlink(path).
From: man remove.
Good Luck ;)
unlink is not unix-specific, i don't know why people're saying that. see io.h. although you'll probably have to do something like
#define unlink _unlink
http://msdn.microsoft.com/en-us/library/1c3tczd6%28v=VS.100%29.aspx
http://msdn.microsoft.com/en-us/library/2da4hk1d%28v=VS.100%29.aspx
remove() is part of the C++ standard (N4860 29.11.14.30). unlink() is not.
Related
In my codebase, I ve got a lot function declarations with void-pointers as argument
void my_func(void* my_void_pointer)
I need to find all places in my sources
where my_func is called
(more importantly) with which type as argument.
For example calls like:
int* intpt=new int(10);
my_func(intpt);
or
char* charpt = new char('a');
my_func(charpt);
I need this because usually my_func does a reinterpret_cast to some self defined types and I would like to find out what possibly could go wrong if for example my byteorder changes.
I have already had a look at gcc_xml, but with this tool I can only find out which functions are defined with which arguments/argument types. Of course I could now grep the sources for function calls of such functions, but I still do not know with which types they are called with. Any idea which tool to start with?
Start with your compiler. Go and break the prototype and implementation of my_func by renaming it to Xmy_func (or any other change) and recompile... the compiler will tell you every place it's used.
Rename the argument to a non-pointer and recompile. You should get errors like cannot convert int* to int or cannot convert char* to int wherever your function is called.
You could write a small utility using Clang Tooling.
If you working in *nix terminal, you can try something like this:
// in project root folder
// you can replace *.cpp with *.h or *.hpp etc
for i in $(find . -type f -name "*.cpp"); do \
grep -Hn "my_func" $i; \
done;
Option 1. Use the following command to serach the occureneces of my_func in source directory.
grep "my_func(" *
Option 2. Use of source navigator. Open the source in source navigator and search the function name "my_func".
With a modern IDE, such as Eclipse CDT, you can search for all occurrences of each of your functions and explore call sites. Note however that Eclipse CDT doesn't appear to be able to distinguish overloads when searching, as its Java counterpart does.
Is there a cross-platform function in Qt that is equivalent to the MoveFile function in Windows and the mv command in Linux?
Sure, QDir::rename() following the old Unix / POSIX tradition of calling this rename.
Which makes sense if you think of a file with its complete path: the underlying inodes just get assigned a different path/file label.
You would use QDir::rename() but be beware of the special cases when rename() can fail:
On most file systems, rename() fails
only if oldName does not exist, if
newName and oldName are not on the
same partition or if a file with the
new name already exists. However,
there are also other reasons why
rename() can fail. For example, on at
least one file system rename() fails
if newName points to an open file.
QUrlOperator::copy() is an alternative to QDir::rename() that may also work for you.
I am trying to validate a directory with C++.
http://php.net/manual/en/function.is-readable.php
bool is_readable ( string $filename )
Tells whether a file (or directroy) exists and is readable.
What would be the equivalent of the above in C++?
I am already using the boost/filesystem library to check that the directory exists.
I have checked the documentation:
http://www.boost.org/doc/libs/1_44_0/libs/filesystem/v3/doc/index.htm
but I cannot find the equivalent of PHP's is_readable().
If it is not possible with the boost/filesystem library, what method would you use?
Since you've tagged the question "Linux", there is a POSIX function to check if the file is readable/writable/executable by the user of the current process. See man 2 access.
int access(const char *pathname, int mode);
For example,
if (-1 == access("/file", R_OK))
{
perror("/file is not readable");
}
Alternatively, if you need portability, try to actually open the file for reading (e.g. std::ifstream). If it succeeds, the file is readable. Likewise, for directories, use boost::filesystem::directory_iterator, if it succeeds, directory is readable.
Most operating systems provide stat().
In C++, on Linux, how can I write a function to return a temporary filename that I can then open for writing?
The filename should be as unique as possible, so that another process using the same function won't get the same name.
Use one of the standard library "mktemp" functions: mktemp/mkstemp/mkstemps/mkdtemp.
Edit: plain mktemp can be insecure - mkstemp is preferred.
tmpnam(), or anything that gives you a name is going to be vulnerable to race conditions. Use something designed for this purpose that returns a handle, such as tmpfile():
#include <stdio.h>
FILE *tmpfile(void);
The GNU libc manual discusses the various options available and their caveats:
http://www.gnu.org/s/libc/manual/html_node/Temporary-Files.html
Long story short, only mkstemp() or tmpfile() should be used, as others have mentioned.
man tmpfile
The tmpfile() function opens a unique temporary file in binary
read/write (w+b) mode. The file will be automatically deleted when it
is closed or the program terminates.ote
mktemp should work or else get one of the plenty of available libraries to generate a UUID.
The tmpnam() function in the C standard library is designed to solve just this problem. There's also tmpfile(), which returns an open file handle (and automatically deletes it when you close it).
You should simply check if the file you're trying to write to already exists.
This is a locking problem.
Files also have owners so if you're doing it right the wrong process will not be able to write to it.
How do I find the 'temp' directory in Linux? I am writing a platform neutral C++ function that returns the temp directory. In Mac and Windows, there is an API that returns these results. In Linux, I'm stumped.
Check following variables:
The environment variable TMPDIR
The value of the P_tmpdir macro
If all fails try to use the directory /tmp.
You can also use tempnam function to generate a unique temporary file name.
Edit: Fair point from the commenter. tmpnam isn't a good choice these days; use mktemp/mkstemp instead.
Historical answer: Be POSIX compliant, and use tmpnam (which will give you a full filename in a temporary location).
Use the value of the $TMPDIR environment variable, and if that doesn't exist, use /tmp.
The accepted sequence, specifically from a GNU standpoint, is:
Check the environmental variable TMPDIR (getenv("TMPDIR")) only if
the program is not running as SUID/SGID (issetugid() == 0)
Otherwise use P_tmpdir if it is defined and is valid
and finally, should those fail, use _PATH_TMP available from paths.h
If you are adding an extension or module, check to see if the core provides a function for this purpose. For example, PHP exports php_get_temporary_directory() from main/php_open_temporary_file.h.
In standard c, you could try: P_tmpdir