- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试制作一个复制程序,允许我将文件从当前目录复制到另一个目录。到目前为止,我正在使用 open()、creat()、read()、write()。
例如:到目前为止,我只能将文件复制到当前目录。
我有一个文件夹名称 A_folder 和一个文件名称 file_1 我想将 file_1 从我当前目录复制到 A_folder .
有人可以帮帮我吗?
代码和图片如下所示
我现在能做的:./copy file1 copy_file
我想要的是:./copy file1 ./folder copy_file
最佳答案
当给定 A_folder
和 file_1
时,您需要一个辅助函数来构造您需要的路径 A_folder/file_1
。
这种函数的一个典型例子是
char *combine_path(const char *dir, const char *name)
{
/* Calculate the lengths of the path components.
If the respective parameter is NULL, the length is zero .*/
const size_t dirlen = (dir) ? strlen(dir) : 0;
const size_t namelen = (name) ? strlen(name) : 0;
char *path, *p;
/* We allocate <dir> + '/' + <name> + '\0'. */
path = malloc(dirlen + namelen + 2);
if (!path) {
errno = ENOMEM;
return NULL;
}
/* Let p point to the current position in the
resulting path. */
p = path;
/* If there is a directory part,
copy it, and append a '/' after it. */
if (dirlen > 0) {
memcpy(p, dir, dirlen);
p += dirlen;
*p = '/';
p += 1;
}
/* If there is a name part, copy it. */
if (namelen > 0) {
memcpy(p, name, namelen);
p += namelen;
}
/* Append a NUL char, '\0', to terminate the
dynamically allocated buffer.
This turns it into a C string. */
*p = '\0';
/* Return the pointer to the dynamically-allocated
memory, containing the concatenated paths
as a single string. */
return path;
}
请注意,上述函数返回一个动态分配的副本,因此您应该在不再需要时free()
该结果。
我更喜欢更明确的错误检查。考虑:
int copy_file(const char *frompath, const char *topath)
{
struct stat frominfo, toinfo;
char data[BUFFERSIZE];
ssize_t n;
int src, dst, cause;
if (!frompath || !*frompath ||
!*topath || !*topath) {
fprintf(stderr, "copy_file(): NULL or empty file name!\n");
return errno = EINVAL;
}
src = open(frompath, O_RDONLY | O_NOCTTY);
if (src == -1) {
cause = errno;
fprintf(stderr, "%s: Cannot open file: %s.\n", frompath, strerror(cause));
return errno = cause;
}
if (fstat(src, &frominfo) == -1) {
cause = errno;
fprintf(stderr, "%s: Cannot get file statistics: %s.\n", frompath, strerror(cause));
return errno = cause;
}
dst = open(topath, O_WRONLY | O_CREAT | O_EXCL, frominfo.st_mode & 0777);
if (dst == -1) {
cause = errno;
fprintf(stderr, "%s: Cannot create file: %s.\n", topath, strerror(saved_errno));
errno = cause;
}
while (1) {
char *p, *q;
n = read(src, buffer, sizeof buffer);
if (n == 0) {
/* End of input. */
cause = 0;
break;
} else
if (n < 0) {
/* Error (or interrupt, EINTR). */
if (n == -1)
cause = errno;
else
cause = EIO; /* n < -1 is a bug. */
fprintf(stderr, "%s: Read error: %s.\ņ", frompath, strerror(cause));
break;
}
p = buffer;
q = n;
cause = 0;
while (p < q) {
n = write(dst, p, (size_t)(q - p));
if (n > 0)
p += n;
else
if (n == -1) {
cause = errno;
break;
else {
/* Bug; should never occur. */
cause = EIO;
break;
}
}
if (cause) {
fprintf(stderr, "%s: Write error: %s.\n", topath, strerror(cause));
break;
}
}
/* Failed? */
if (cause) {
unlink(topath);
return errno = cause;
}
if (fstat(dst, &toinfo) == -1) {
cause = errno;
fprintf(stderr, "%s: Cannot get file information: %s.\n", topath, strerror(cause));
unlink(topath);
return errno = cause;
}
/* from may be a device; if so, its size
will be zero. */
if (frominfo.st_size > 0 &&
frominfo.st_size != toinfo.st_size) {
cause = EIO;
fprintf(stderr, "%s: File size mismatch!\n", topath);
unlink(topath);
return errno = cause;
}
/* Careful closing. */
if (close(src) == -1) {
cause = errno;
fprintf(stderr, "%s: Error closing file: %s.\n", frompath, strerror(cause));
unlink(topath);
return errno = cause;
}
if (close(dst) == -1) {
cause = errno;
fprintf(stderr, "%s: Error closing file: %s.\n", topath, strerror(cause));
unlink(topath);
return errno = cause;
}
/* Success. */
return errno = 0;
}
请注意我如何使用 p
和 q
指针将读取缓冲区内容写入可能不止一个部分的模式。如果源文件是本地的,而目标文件位于某个不稳定的文件系统上,则可能会发生这种情况。没有要求 write()
应该写入整个缓冲区或返回错误代码;短写——只写入给定缓冲区中的一些初始数据——是完全可以的,并且在某些情况下确实会发生。以上是我处理这些问题的首选方式。
许多人考虑这种级别的错误检查——特别是检查 close()
的结果值,因为在这个时间点许多操作系统(包括 Linux)从不返回错误——愚蠢或者至少是偏执狂。
我个人认为这种错误检查“稳健”,因为我希望我的代码能够告诉我,作为用户,如果发生了任何不幸的事情;我绝对不希望它只是假设一切顺利,我希望它对此偏执。 (我并不是说 OP 的代码不检查错误代码;我只是说这个版本更加谨慎和直言不讳。)
关于c - Unix:将文件从当前目录复制到 C 中的另一个目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43660314/
我正在为我的程序编写安装脚本,它应该在 Linux/Unix 操作系统上运行。以下文件的默认目录是什么: 可执行文件(程序)。程序应通过从命令行键入其名称来执行。 共享库。 第三方共享库(程序未开源,
我有一堆用户、组和应用程序注册,我的 MVC 应用程序使用 AAD 数据进行身份验证和授权。是否可以将 Azure Active Directory 从一个租户(目录)迁移到另一个租户(目录)?如果可
查看 cljsbuild 文档 https://github.com/emezeske/lein-cljsbuild :cljsbuild { :builds [{ ; The
忽略已经版本控制的文件 如果你不小心添加了一些应该被忽略的文件,你如何将它们从版本控制中去除而不会丢失它们?或许你有自己的IDE配置文件,不是项目的一部分,但将会花费很多时间使之按照自己的方式工作。
我想使用\tableofcontents 命令,但没有目录从新页面开始或在末尾创建新页面,并且所有内容都是单倍行距。我怎样才能做到这一点?我假设使用 tocloft,但有哪些选择? 谢谢 最佳答案 试
我有一些 javascript 菜单代码,可以在单独的目录中正常工作。但是,当我尝试从同一目录中调用相同的 .js 文件时,它不会看到这些文件。 以下内容来自另一个目录: script type="t
我有这样的路径: /my/path/to/important_folder 在同一级别上,我还有其他文件和文件夹想要在达到与 important_folder 相同的级别时列出。 我的文件夹可能更深,
1、获取文件路径实现 1.1 获取当前文件路径 ? 1
我正在使用最新版本的 NTEmacs。 我写了一个名为“.dir-locals.el”的文件,如下所示。 ((nil . ((tab-width . 8) (fill-column .
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
在我的 .vimrc 中有这些行 :set foldmethod=marker :set foldmarker=SECTION:,ENDSECTION: 用于自定义代码折叠。在我的文件中,相关语言的注
在 fish 中: for x in * echo $x end *这里包括所有目录和文件,如何只列出文件(或目录)? 最佳答案 fish 没有很多花哨的通配语法。但是,目录可以像这样迭代: f
这是我的目录结构: ├── src │ ├── helpers │ │ ├── __init__.py │ │ ├── foo.py │ │ └── bar.py │
我想递归重命名文件夹/目录名称并找到 this solution所以。但是这个命令没有效果 find . -type f -exec rename 's/old/new/' '{}' \; 这是一个正
我想在相册中创建一个文件夹,并希望将图像保存在创建的相册中。 这可能吗?有什么办法可以做到这一点吗? 我已经搜索过,大多数人都说这是不可能的。 感谢您的帮助。 最佳答案 您也许可以使用AssetsLi
如何在python中使用用户定义的名称创建临时文件/目录。我知道 tempfile .但是我看不到任何以文件名作为参数的函数。 注意:我需要这个来对包含临时文件的临时目录上的 glob(文件名模式匹配
我在项目中使用JaCoCo Gradle插件。 作为问题的一个示例,我的大部分代码都在com.me.mysoftware包下。 我正在使用代码生成器来生成build/generated/java/..
我正在尝试使用 Gradle 开始运行 jar 文件 我的任务如下所示: task startServer(type: Exec) { workingDir file("${buildDir}/a
如何在 Ant 中定义一个目录集,其中包括两个目录:项目的基目录和子目录“test”? 看起来您无法使用“/”、“.”或“”专门包含目录集的根目录。例如,这包括“./test”,但不包括“.”:
我正在使用 CTAGs 包,它使用 Sublime Text 2 生成两个文件 .tags 和 .tags_sorted_by_file。 那么当我进行项目搜索(CMD + SHIFT + F)时,如
我是一名优秀的程序员,十分优秀!