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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言中 如何删除文件和重命名文件,举个例子可以么相关的知识,希望对你有一定的参考价值。

函数名: 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;
参考技术A int remove( char *path );
int rename( char *oldname, char *newname );

此为正解!
main()

char str[10];
strcpy(str,"test.txt");
if(remove(str)==-1)
printf("remove file error.\n");
else printf("File was removed.\n");


main()

char oldname[20],newname[20];
clrscr();
printf("input oldname:\n");
gets(oldname);
printf("input newname:\n");
gets(newname);
if(rename(oldname,newname)==0)
printf("File was renamed.\n");
else
printf("File rename error");
getch();

PHP 使用按钮下载、删除和重命名文件

【中文标题】PHP 使用按钮下载、删除和重命名文件【英文标题】:PHP download, delete and rename files with buttons 【发布时间】:2016-12-20 07:42:40 【问题描述】:

所以我正在使用 php 创建一个小型文件管理器,它将在表格中列出我服务器上一个目录中的所有可用文件,并为我提供下载或删除选项。

这是我的代码:

<html>
<head>
    <title>My first PHP Page</title>
</head>

<body>
    <iframe id="my_iframe" style="display:none;"></iframe>
    <table border="1">
    <?php
        $dir = 'uploads';
        $files = scandir($dir);
        sort($files);
        $count = -1 ;
        foreach ($files as $file) 
            if ($file != '.' && $file != '..') 
                $str_URL = "uploads".$file;
                echo "<tr>";
                echo "<td>";
                echo $count;
                echo "</td>";
                echo "<td>";
                echo $file;
                echo "</td>";
                echo "<td>";
                echo "<input type='submit' value='Download'/>";
                echo "</td>";
                echo "<td>";
                echo "<input type='submit' value='Delete'/>";
                echo "</td>";
                echo "<td>";
                echo "<input type='submit' value='Rename'/>";
                echo "</td>";
                echo "</tr>";
            
            $count++;
        
    ?>
</table>
</body>
</html>

我可以列出文件,但无法让“下载”和“删除”按钮正常工作。

非常感谢任何帮助,并在此先感谢您。

【问题讨论】:

你在使用提交吗?你的表格在哪里 【参考方案1】:

看我的代码

<html>
<head>
    <title>My first PHP Page</title>
</head>

<body>
    <iframe id="my_iframe" style="display:none;"></iframe>
    <table border="1">
    <?php
        $dir = 'uploads';
        $files = scandir($dir);
        sort($files);
        $count = -1 ;
        foreach ($files as $file) 
        $v_download = "download_".$count;
        $v_delete = "delete_".$count;
        $v_rename = "rename_".$count;
            if ($file != '.' && $file != '..') 
                $str_URL = "uploads".$file;
                echo "<tr>";
                echo "<td>";
                echo $count;
                echo "</td>";
                echo "<td>";
                echo $file;
                echo "</td>";
                echo "<td>";
                echo "<form action='' method='post'><input type='submit' value='Download' name='".$v_download."'/></form>";
                if(isset($_POST[$v_download])) 
                // Your php download code here
                echo "download file : ".$file;
                
                echo "</td>";
                echo "<td>";
                echo "<form action='' method='post'><input type='submit' value='Delete' name='".$v_delete."'/></form>";
                if(isset($_POST[$v_delete])) 
                // Your php delete code here
                echo "delete file : ".$file;
                
                echo "</td>";
                echo "<td>";
                echo "<form action='' method='post'><input type='submit' value='Rename' name='".$v_rename."'/></form>";
                if(isset($_POST[$v_rename])) 
                // Your php rename code here
                echo "rename file : ".$file;
                
                echo "</td>";
                echo "</tr>";
            
            $count++;
        
    ?>
</table>
</body>
</html>

【讨论】:

以上是关于c语言中 如何删除文件和重命名文件,举个例子可以么的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Android 中删除和重命名文件?

如何使用shell复制和重命名多个文件

PHP 使用按钮下载、删除和重命名文件

在github中移动和重命名文件

怎样把自己编写的java程序变成jar可执行文件?能举个例子么?

文件复制和重命名