在 Ubuntu 中创建一个文件作为 C 中的可执行程序
Create a file as executable program in C in Ubuntu
我的程序通过TCP套接字接收一个可执行的二进制文件。
我需要将此文件作为可执行程序保存到硬盘中。已成功接收文件,但问题是默认文件属性设置为不可执行。
如何在 Ubuntu 中将文件的属性更改为 C 中的可执行文件?
谢谢问候机器人
int chmod(const char *path, mode_t mode)
和int fchmod(int fd, mode_t mode) ?
怎么样
apropos chmod
man 2 chmod
最基本的例子:
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]){
char * fpath = "/path/to/binary";
int ret=0;
if(ret = chmod(fpath, S_IRUSR|S_IXUSR) < 0){
perror("chmod failed");
exit(1);
}
printf("chmod okn");
exit(0);
}
您如何创建和写入文件? 如果您知道它将是可执行的,请首先使用正确的模式制作文件。
int fd = open("path/to/file", O_WRONLY | O_CREAT, 0777);
除非umask
剥离可执行位(通用值是0022
和0002
,其中不保留可执行位),否则path/to/file
最初将创建可执行文件。
您可以使用
chmod
更改文件模式。有关详细信息,请阅读手册页( man 2 chmod
)(与 shell 命令chmod
大致相同)。
相关文章:
- 如何用共享库替换可执行C++程序的功能?
- c ++:如何为每个特征制作特定的二进制文件(可执行文件)?
- 如果我在 Linux 上更改C++动态共享库,而我的可执行程序在其上使用,会发生什么
- 在英特尔C++编译器 (Linux) 的配置搜索路径中找不到文件可执行文件
- 调试和发布库及其可执行程序
- 在运行时动态更新二进制文件(可执行文件)而不停止的任何类型的方法
- 使用 RS232 电缆将可执行程序发送到硬件(微处理器)
- 调用Android内部的C++可执行程序
- c++可执行程序的分布式计算
- 在 Ubuntu 中创建一个文件作为 C 中的可执行程序
- 源代码构建中的 SCons 仅适用于可执行程序,不适用于目标文件
- 当我们不给 C 可执行程序参数时如何处理异常
- C++11与OpenMP结合使用,可执行程序速度较慢
- 从QtCreator导出可执行程序
- c++虚拟函数和可执行程序
- 启动使用附加库的可执行程序
- 英特尔至强Phi -运行多个单线程可执行程序
- 如何在c++中创建独立的可执行程序
- valgrind/callgrind可以在发布的可执行c++程序上工作吗?
- 使用nvcc的可执行程序比使用gcc/g++和OpenCL的可执行文件大