簡體   English   中英

Unix:將文件從當前復制到C中的另一個目錄

[英]Unix: Copy file from current to another directory in C

我正在嘗試制作一個允許我將文件從當前目錄復制到另一個目錄的復制程序。 到目前為止,我正在使用open(),creat(),read(),write()。

例如:到目前為止,我只能將文件復制到當前目錄。

我有一個文件夾名稱A_folder和一個文件名file_1 ,我想將文件_1從當前目錄復制到A_folder

有人可以幫幫我嗎?

代碼和圖像如下所示

我現在可以做什么:./copy file1 copy_file

我想要的是:./copy file1 ./folder copy_file

  1. 當給定A_folderfile_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()

  2. 我更喜歡更明確的錯誤檢查。 考慮:

     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; } 

    請注意該模式,我如何使用pq指針以可能不止一個部分的方式寫入讀取緩沖區的內容。 如果源文件是本地文件,而目標文件在某些​​文件系統上,則會發生這種情況。 並不要求 write()應該寫入整個緩沖區或返回錯誤代碼。 短寫(只寫給定緩沖區中的一些初始數據)完全可以,並且在某些情況下確實會發生。 以上是我處理這些問題的首選方式。

    許多人認為這種級別的錯誤檢查-特別是檢查close()的結果值,因為此時許多操作系統(包括Linux)從不向那里返回錯誤-愚蠢或至少偏執。

    我個人認為這種錯誤檢查“健壯” ,因為我希望我的代碼以用戶的身份告訴我是否發生了任何不幸的事情。 我絕對不希望它只是假設一切都很好,我希望它對此感到偏執。 (我並不是說OP的代碼不會檢查錯誤代碼;我只是說這個版本更加謹慎和直言不諱。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM