c语言删除文件remove,如何在不使用C程序中的remove系统调用的情况下删除文件?...

I've been curious how rem in Linux works and trying to write my own C code that can delete a file but when I searched for the answer, I only got the programs that were using remove() system call.

Is there any other way of doing it without using system call like writing your own code to do the job?

I've accomplished copying file through C filing but can't find a solution to delete a file through C.

解决方案

If you want to delete a file use the

remove

function. If you want to have a look behind the scenes of the standard library, you may download the source of the glibc (e.g.) and have a look at the implementation. You will see that actually a INTERNAL_SYSCALL will be performed on linux os:

result = INTERNAL_SYSCALL (unlink, err, 1, file);

(from /sysdeps/unix/sysv/linux/unlinkat.c from the debian eglibc-2.15 package)

If you want to go further and even not use that syscall you will have to implement your own file system logic since the file system syscall just gives an abstraction layer to different filesystems.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Python C API接口对sys.path和sys.modules进行写入和删除时,需要注意线程安全性。因为这些变量是全局的,如果多个线程同时修改它们,可能会导致竞争条件和数据不一致性问题。 为了解决这个问题,可以使用Python提供的线程锁来保证线程安全。以下是一个示例代码,演示如何在C语言使用Python C API接口对sys.path进行线程安全的写入和删除: ```c #include <Python.h> #include <thread> // 定义全局锁 static PyGILState_STATE gstate; static PyThreadState *main_thread_state = nullptr; static PyThreadState *current_thread_state = nullptr; static PyObject* add_path_to_sys(PyObject* self, PyObject* args) { const char* path; if (!PyArg_ParseTuple(args, "s", &path)) { return nullptr; } // 获取全局锁 gstate = PyGILState_Ensure(); current_thread_state = PyThreadState_Get(); if (current_thread_state == nullptr) { PyGILState_Release(gstate); return nullptr; } // 获取主线程状态 if (main_thread_state == nullptr) { main_thread_state = PyEval_SaveThread(); } // 获取全局变量 sys.path PyObject* sys_module = PyImport_ImportModule("sys"); if (sys_module == nullptr) { PyGILState_Release(gstate); return nullptr; } PyObject* sys_dict = PyModule_GetDict(sys_module); PyObject* sys_path = PyDict_GetItemString(sys_dict, "path"); if (sys_path == nullptr) { Py_DECREF(sys_module); PyGILState_Release(gstate); return nullptr; } // 添加路径到 sys.path PyObject* new_path = PyUnicode_FromString(path); PyList_Append(sys_path, new_path); Py_DECREF(new_path); // 释放资源 Py_DECREF(sys_module); PyGILState_Release(gstate); return Py_None; } static PyObject* remove_path_from_sys(PyObject* self, PyObject* args) { const char* path; if (!PyArg_ParseTuple(args, "s", &path)) { return nullptr; } // 获取全局锁 gstate = PyGILState_Ensure(); current_thread_state = PyThreadState_Get(); if (current_thread_state == nullptr) { PyGILState_Release(gstate); return nullptr; } // 获取主线程状态 if (main_thread_state == nullptr) { main_thread_state = PyEval_SaveThread(); } // 获取全局变量 sys.path PyObject* sys_module = PyImport_ImportModule("sys"); if (sys_module == nullptr) { PyGILState_Release(gstate); return nullptr; } PyObject* sys_dict = PyModule_GetDict(sys_module); PyObject* sys_path = PyDict_GetItemString(sys_dict, "path"); if (sys_path == nullptr) { Py_DECREF(sys_module); PyGILState_Release(gstate); return nullptr; } // 从 sys.path 删除路径 for (Py_ssize_t i = 0; i < PyList_Size(sys_path); i++) { PyObject* item = PyList_GetItem(sys_path, i); const char* str = PyUnicode_AsUTF8(item); if (strcmp(str, path) == 0) { PyList_SetSlice(sys_path, i, i+1, nullptr); break; } } // 释放资源 Py_DECREF(sys_module); PyGILState_Release(gstate); return Py_None; } static PyMethodDef my_methods[] = { {"add_path_to_sys", add_path_to_sys, METH_VARARGS, "add path to sys.path"}, {"remove_path_from_sys", remove_path_from_sys, METH_VARARGS, "remove path from sys.path"}, {NULL, NULL, 0, NULL} }; static struct PyModuleDef my_module = { PyModuleDef_HEAD_INIT, "mymodule", "Python extension module", -1, my_methods }; PyMODINIT_FUNC PyInit_mymodule(void) { return PyModule_Create(&my_module); } ``` 在上述代码,我们使用了PyGILState_Ensure()函数来获取全局锁,保证线程安全。同时,我们还保存了主线程状态和当前线程状态,用于后续的操作。 在add_path_to_sys函数,我们先获取sys.path变量,然后将新的路径添加到该变量。在remove_path_from_sys函数,我们遍历sys.path变量,找到要删除的路径,然后将其从列表删除。 需要注意的是,由于Python C API是与Python解释器紧密绑定的,所以我们需要在主线程调用PyEval_SaveThread()函数,将主线程状态保存下来。这可以保证我们在C代码使用Python C API时,能够正确地访问全局变量和调用Python函数。 最后,我们将上述代码编译为动态链接库(例如mymodule.so),然后在C程序通过dlopen()函数加载该动态链接库,并调用的函数来修改sys.path变量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值