搜档网
当前位置:搜档网 › C语言怎么拷贝和删除文件

C语言怎么拷贝和删除文件

C语言中怎么拷贝,删除,文件以及改名?(转)2007-07-18 15:55函数名: rename
功 能: 重命名文件
用 法: int rename(char *oldname, char *newname);
程序例:

#include

int main(void)
{
char oldname[80], newname[80];

/* prompt for file to rename and new name */
printf("File to rename: ");
gets(oldname);
printf("New name: ");
gets(newname);

/* Rename the file */
if (rename(oldname, newname) == 0)
printf("Renamed %s to %s.\n", oldname, newname);
else
perror("rename");

return 0;
}



--------------------------------------------------------

函数名: remove
功 能: 删除一个文件
用 法: int remove(char *filename);
程序例:

#include

int main(void)
{
char file[80];

/* prompt for file name to delete */
printf("File to delete: ");
gets(file);

/* delete the file */
if (remove(file) == 0)
printf("Removed %s.\n",file);
else
perror("remove");

return 0;
}



--------------------------------------------------------

拷贝的话,
要么字节写一个函数;

或者使用 win API: CopyFile

--------------------------------------------------------

https://www.sodocs.net/doc/7d16395531.html,/Article/cxsj/fpro/200612/7004.html

--------------------------------------------------------

/* Chapter 1. Basic cp file copy program. C library Implementation. */
/* cp file1 file2: Copy file1 to file2. */

#include
#include
#include
#define BUF_SIZE 256

int main (int argc, char *argv [])
{
FILE *in_file, *out_file;
char rec [BUF_SIZE];
size_t bytes_in, bytes_out;
if (argc != 3) {
printf ("Usage: cp file1 file2\n");
return 1;
}
in_file = fopen (argv [1], "rb");
if (in_file == NULL) {
perror (argv [1]);
return 2;
}
out_file = fopen (argv [2], "wb");
if (out_file == NULL) {
perror (argv [2]);
return 3;
}

/* Process the input file a record at a time. */

while ((bytes_in = fread (rec, 1, BUF_SIZE, in_file)) > 0) {
bytes_out = fwrite (rec, 1, bytes_in, out_file);
if (bytes_out != bytes_in) {
perror ("Fatal write error.");
return 4;
}
}

fclose (in_file);
fclose (out_file);
return 0;
}

--------------------------------------------------------

这个基本上是系统API,虽然C库里也有。

--------------------------------------------------------

有个叫dos.h的头文件,这些操作的函数都有。你可以查查看。

--------------------------------------------------------

如果自己写这些文件的删除,重命名等函数呢?应该怎么实现呢?

--------------------------------------------------------

需要你對文件系統有瞭解了.


相关主题