c语言文件重命名函数 c语言修改文件名函数

c语言中 如何删除文件和重命名文件,举个例子可以么函数名: rename
功能: 重命名文件
用法: int rename(char *oldname, char *newname);
程序例:
#include stdio.h
int main(void)
{
【c语言文件重命名函数 c语言修改文件名函数】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
--------------------------------------------------------
--------------------------------------------------------
/* 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;
}
C语言中 rename 的用法rename函数功能是给一个文件重命名 , 用该函数可以实现文件移动功能,把一个文件的完整路径的盘符改一下就实现了这个文件的移动 。具体参见下面的程序示例说明 。
头文件:在Visual
C6.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])
)
printf("error!\n");
else
printf("ok!\n");
return
0;
}
如何在C语言编程里面修改源文件名字C修改文件名:使用rename函数 。
rename函数:功能描述: 改变文件的名称或者位置,如果目标已存在 , 将被自动覆盖 。用法:#include stdio.hint rename(const char *oldpath, const char *newpath);参数:
oldpath:旧文件名 。newpath:新文件名或者新位置 。
具体可以分以下2种情况:
1、修改单个文件
直接使用rename即可 。
2、批量修改文件(如:按一定规则修改某目录下所有文件)
需要使用opendir遍历目录,然后修改该目录下文件 。下面提供一个简单的例子 。
void ModFilesName(const char *pcszPath)
{
char szPathFile[1024] = {0};//路径 文件名
DIR *dir_p;
struct dirent *direntp;
struct stat entryInfo;
//文件目录不存在,则创建
if(stat(pcszPath, entryInfo)0)
{
printf("Auto create folder:%s\n", pcszPath);
mkdir(pcszPath, 0755);
}
if ((dir_p = opendir (pcszPath)) == NULL)
{
return;
}
while ((direntp = readdir (dir_p)) != NULL)
{
//组合完整路径
sprintf(szPathFile, "%s/%s", pcszPath, direntp-d_name);
//判断文件是否是目录
if(lstat(szPathFile, entryInfo) == 0)
{
if(S_ISDIR(entryInfo.st_mode))
{
continue;//忽略目录
}
rename(szPathFile, 你要修改成的文件名);
}
} // while ( ...
closedir (dir_p);
}
推荐一片文章:
希望能帮助到你,你的好评是我前进的动力!谢谢!
c语言中重命名函数指的是什么?请举个例子函数定义不可以嵌套的意思是:不可以在一个函数内定义另一个函数例如voidfun(){voidfun1(){}}函数调用可以嵌套,函数调用只有函数名和参数fun()这叫调用例如voidfun(){fun1();}这样是可以的
用C语言编程修改文件名(C也行)修改文件名 , 可要调用操作系统提供的API函数,比如Windows上的MoveFile(),也可以直接调用cmd中已提供的重命名命令——rename 。下面的示例代码,调用rename命令来重名命文件名 。
#include stdio.h
#include stdlib.h
#include string.h
int main(int ac, char *pav[])
{
if (ac!=3) {
printf("程序名 要重命名的文件路径 新的文件名\n");
printf("示例:test.exe 1.txt 2.txt\n");
return 0;
}
if (access(pav[1], 0) !=0) {
printf("不存在该文件\n");
return 0;
}
char szcmd[256] = "cmd /c rename ";
strcat(szcmd, pav[1] );
strcat(szcmd, " ");
strcat(szcmd, pav[2]);
system(szcmd);
return 0;
}
c语言文件重命名函数的介绍就聊到这里吧 , 感谢你花时间阅读本站内容 , 更多关于c语言修改文件名函数、c语言文件重命名函数的信息别忘了在本站进行查找喔 。

    推荐阅读