1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > c语言文件怎么重命名文件 c语言中 如何删除文件和重命名文件 举个例子可以么...

c语言文件怎么重命名文件 c语言中 如何删除文件和重命名文件 举个例子可以么...

时间:2019-12-22 02:03:48

相关推荐

c语言文件怎么重命名文件 c语言中 如何删除文件和重命名文件 举个例子可以么...

满意答案

takurachin

.05.27

采纳率:45%等级:12

已帮助:8700人

函数名: 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

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

/Article/cxsj/fpro/12/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;

}

01分享举报

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。