c语言 删除指定文件

发布网友

我来回答

4个回答

热心网友

  C语言删除指定文件或目录,参考代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <direct.h>
#include <errno.h>

//判断是否是".."目录和"."目录
inline bool is_special_dir(const char *path)
{
    return strcmp(path, "..") == 0 || strcmp(path, ".") == 0;
}

//判断文件属性是目录还是文件
inline bool is_dir(int attrib)
{
    return attrib == 16 || attrib == 18 || attrib == 20;
}

//显示删除失败原因
inline void show_error(const char *file_name = NULL)
{
    errno_t err;
    _get_errno(&err);
    switch(err)
    {
        case ENOTEMPTY:
            printf("Given path is not a directory, the directory is not empty, or the directory is either the current working directory or the root directory.\n");
            break;
        case ENOENT:
            printf("Path is invalid.\n");
            break;
        case EACCES:    
            printf("%s had been opend by some application, can't delete.\n", file_name);
            break;
    }
}

inline void get_file_path(const char *path, const char *file_name, char *file_path)
{
    strcpy_s(file_path, sizeof(char) * _MAX_PATH, path);
    file_path[strlen(file_path) - 1] = '\0';
    strcat_s(file_path, sizeof(char) * _MAX_PATH, file_name);
    strcat_s(file_path, sizeof(char) * _MAX_PATH, "\\*");
}

//递归搜索目录中文件并删除
inline void delete_file(char *path)
{
    _finddata_t dir_info;
    _finddata_t file_info;
    intptr_t f_handle;
    char tmp_path[_MAX_PATH];
    if((f_handle = _findfirst(path, &dir_info)) != -1)
    {
        while(_findnext(f_handle, &file_info) == 0)
        {
            if(is_special_dir(file_info.name))
                continue;
            if(is_dir(file_info.attrib))//如果是目录,生成完整的路径
            {    
                get_file_path(path, file_info.name, tmp_path);
                delete_file(tmp_path);//开始递归删除目录中的内容
                tmp_path[strlen(tmp_path) - 2] = '\0';
                if(file_info.attrib == 20)
                    printf("This is system file, can't delete!\n");
                else
                {
                    //删除空目录,必须在递归返回前调用_findclose,否则无法删除目录
                    if(_rmdir(tmp_path) == -1)
                    {
                        show_error();//目录非空则会显示出错原因
                    }
                }
            }
            else
            {
                strcpy_s(tmp_path, path);
                tmp_path[strlen(tmp_path) - 1] = '\0';
                strcat_s(tmp_path, file_info.name);//生成完整的文件路径
                
                if(remove(tmp_path) == -1)
                {
                    show_error(file_info.name);
                }
                
            }
        }
        _findclose(f_handle);//关闭打开的文件句柄,并释放关联资源,否则无法删除空目录
    }
    else
    {
        show_error();//若路径不存在,显示错误信息
    }
}

int main(int argc, char **argv)
{
    delete_file("C:\\Documents and Settings\\Administrator\\Local Settings\\Temporary Internet Files\\*");
    system("pause");
    return 0;
}

热心网友

用c语言删除某个文件中的一行内容的原理就是
将该行的末尾和
开头找到,
然后将
改行的末尾

热心网友

可以去MSDN里面查函数.

Delete a file.

int remove( const char *path );

Example

/* REMOVE.C: This program uses remove to delete test.dat. */

#include <stdio.h>

void main( void )
{
if( remove( "D:\\test\\test.dat" ) == -1 )
perror( "Could not delete 'test.dat'" );
else
printf( "Deleted 'test.dat'\n" );
}

热心网友

#include <stdlib.h>
#include <stdio.h>

int main(void)
{

if(!remove("d:\\test\\test.dat")) puts("Success");
else puts("Fail");
return 0;
}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com