简体   繁体   English

Linux C ++上的系统命令失败

[英]System command failing on Linux C++

In my program I am copying an executable file from one location to another, and then execute the copied file. 在我的程序中,我将一个可执行文件从一个位置复制到另一个位置,然后执行复制的文件。 When the copied file is executed I get a "permission denied" error. 执行复制的文件后,出现“权限被拒绝”错误。 But if I restart my program then the file gets executed without a problem. 但是,如果我重新启动程序,则文件将被执行而不会出现问题。 Can someone please help me with the problem? 有人可以帮我解决这个问题吗? The code below is simple, but demonstrates the problem. 下面的代码很简单,但是演示了该问题。

void copyFile(string _from, string _to)
{
    std::ifstream  src(_from.c_str());
    std::ofstream  dst(_to.c_str());

    dst << src.rdbuf();
}

int main()
{
    string original("./exe_file");
    string dest_file("./exe_dir/exefile");

    system("./exe_dir/exefile");  //Fails on first run because exe_dir does not exist.

    //mkdir and copy the file.
    mkdir("./exe_dir",S_IRWXO | S_IRWXU | S_IRWXG);
    copyFile(original, dest_file);

    //Open the file and close it again to flush the attribute cache.
    int fd = open(dest_file.c_str(),O_RDONLY);
    close(fd);

    //The line below fails with system error code 2 (Permission denied) on exefile.
    return system("./exe_dir/exefile");
{

I used 'chmod 777 exe_file' on the original file before executing the program, and after running this program the destination also has the same access rights. 在执行程序之前,我在原始文件上使用了“ chmod 777 exe_file”,并且在运行该程序之后,目标位置也具有相同的访问权限。 I can execute it manually just fine. 我可以手动执行它。 And every subsequent run of the program is successful. 程序的每个后续运行均成功。 Why does it fail on the first run? 为什么第一次运行失败?

You should close file you've created. 您应该关闭已创建的文件。

See cplusplus.com: std::ifstream::close 参见cplusplus.com:std :: ifstream :: close

Coderz, no idea what problems you are experiencing with your IDE but this works fine for me. Coderz,不知道您的IDE遇到什么问题,但这对我来说很好。

#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cstdlib>

using namespace std;

void copyFile(string _from, string _to)
{
    std::ifstream  src(_from.c_str());
    std::ofstream  dst(_to.c_str());

    dst << src.rdbuf();
}

int main()
{
    string original("./exe_file");
    string dest_file("./exe_dir/exefile");

    system("./exe_dir/exefile");

    if (mkdir("./exe_dir", S_IRWXO | S_IRWXU | S_IRWXG))
        perror("mkdir");

    copyFile(original, dest_file);

    if (chmod("./exe_dir/exefile", S_IRWXU | S_IRWXG | S_IRWXO) == -1)
        perror("chmod");

    return system("./exe_dir/exefile");
}

Note that exe_file is a simple Hello World binary and the results are 请注意,exe_file是一个简单的Hello World二进制文件,结果为

sh: 1: ./exe_dir/exefile: not found
Hello World

where the copied file is 复制的文件在哪里

-rwxrwxrwx  1 duck duck 18969 May  9 19:51 exefile

within directory 在目录内

drwxrwxr-x 2 duck duck   4096 May  9 19:51 exe_dir

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM