int main()
{
rename([老名字],[新名字])
return 0
}
rename函数功能是给一个文件重命名,用该函数可以实现文件移动功能,把一个文件的完整路径的盘符改一下就实现了这个文件的移动。具体参见下面的程序示例说明。头文件:在Visual
C++6.0中用stdio.h或者io.h
用
法:
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
}
执行过程:
File
to
rename:
D:\\in.dat
New
name:
G:\\in.dat
Renamed
D:\\in.dat
to
G:\\in.dat.
这样就实现了in.dat从D盘移动到G盘。
在unix或linux系统中:
#include
int
rename(const
char
*oldname,
const
char
*newname)
函数说明
(1)
如果oldname为一个文件而不是目录,那么为该文件更名。在这种情况下,如果newname作为一个目录已存在,则它不能重命名一个目录。如果newname已存在,而且不是一个目录,则先将其删除然后将oldname更名为newname。对oldname所在目录以及newname所在的目录,调用进程必须具有写许可权,因为将更改这两个目录。
(2)
如若oldname为一个目录,那么为该目录更名。如果newname已存在,则它必须是一个目录,而且该目录应当是空目录(空目录指的是该目录中只有.
和..
项)。如果newname存在(而且是一个空目录),则先将其删除,然后将oldname更名为newname。另外,当为一个目录更名时,newname不能包含oldname作为其路径前缀。例如,不能将/usr更名为/usr/foo/testdir,因为老名字(
/usr/foo)是新名字的路径前缀,因而不能将其删除。
(3)
作为一个特例,如果oldname和newname引用同一文件,则函数不做任何更改而成功返回。
返回值
执行成功则返回0,失败返回-1,错误原因存于errno
范例
#include
int
main(int
argc,char
**argv)
{
if(argc
<
3)
{
printf("Usage:
%s
old_name
new_name\n",argv[0])
return
-1
}
printf("%s
=>
%s\n",
argv[1],
argv[2])
if(rename(argv[1],
argv[2])
<
0
)
printf("error!\n")
else
printf("ok!\n")
return
0
}
函数名: rename功 能: 重命名文件
用 法: int rename(char *oldname, char *newname)
程序例:
#include <stdio.h>
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 <stdio.h>
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
--------------------------------------------------------
http://itzs.net/Article/cxsj/fpro/200612/7004.html
--------------------------------------------------------
/* Chapter 1. Basic cp file copy program. C library Implementation. */
/* cp file1 file2: Copy file1 to file2. */
#include <windows.h>
#include <stdio.h>
#include <errno.h>
#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
}