为什么以下系统调用输出不重定向到文件?

时间:2017-09-23 16:13:25

标签: c++ string file system

您正在使用smbclient连接我的c ++代码中的远程服务器,并查找远程连接服务器上是否启用了写权限我将重定向到名为tempfile的文件,如下所示:

命令是

smbclient // server / folder -u user%pass --directory = subfolder -c' put file.txt' >临时文件

此命令的输出是

将文件file.txt设置为\ subfolder \ tempfile.txt(2.0 kb / s)(平均2.0 kb / s)

如果字符串"将文件" 置于重定向文件中 tempfile ,我确认启用其他。但tempfile总是空的,不知道为什么?

以下是代码

#include<iostream>
#include<stdlib.h>
#include<fstream>
#define SMBCLIENT "smbclient "
#define TEMPFILE "tempfile"
#define search  "putting file"
enum errcode
{
EMPTY,
internalError

}errCode;

void getUserCreditentail(std::string &username,std::string &password,std::string &server,std::string &folder,std::string &subfolder)
{
        std::cin>>server;
        std::cin>>folder;
        std::cin>>subfolder;
        std::cin>>username;
        std::cin>>password;

}

void MakeCommand(std::string &command,const std::string server,const std::string folder,const std::string subfolder,const std::string username,const std::string password)
{
        if(server.empty() ||folder.empty() || username.empty() ||  password.empty())
        {
                errCode=EMPTY;
                return;
        }

        command= SMBCLIENT +server +"/"+ folder + " " + "-U"+ " " + username + "%" + password + " " + "--directory=" + subfolder + " " + "-c" + " " + "'put  testfile.txt'" + " " + ">" + TEMPFILE;
}

bool executeCommand(const std::string command)
{

if(!command.empty())
        {
                if(system(command.c_str())<0)
                {
                        std::cout<<"system call fails"<<std::endl;
                        errCode=internalError;
                        return false;
                }

        }

return true;


}

bool checkWritePermission()
{

        std::ifstream fin(TEMPFILE);
        std::string line;

        if (fin.is_open())
        {
                std::string line;

                while(getline(fin,line))
                {
                        if (line.find(search, 0) != std::string::npos)
                        {
                                std::cout << "found: " << search << std::endl;
                                return true;
                        }

                }

        }

        return false;
}
int main()
{
        std::string command;
        std::string username,password;
        std::string server,folder,subfolder;
        getUserCreditentail(username,password,server,folder,subfolder);
        std::cout<<server<<" "<<folder<<" "<<username<<" "<<password<<""<<subfolder<<std::endl;
        MakeCommand(command,server,folder,subfolder,username,password);
        std::cout<<command<<std::endl;
        if(executeCommand(command))
        {
                if(checkWritePermission())
                        std::cout<<"Write permission is enable"<<std::endl;
                else
                        std::cout<<"write permission is disabled"<<std::endl;

        }

        return 0;
}

1 个答案:

答案 0 :(得分:1)

使用 2&gt;&amp; 1

将stderr和stdout重定向到文件

更改命令如下

command= SMBCLIENT +server +"/"+ folder + " " + "-U"+ " " + username + "%" + password + " " + "--directory=" + subfolder + " " + "-c" + " " + "'put  testfile.txt'" + " " + ">" + TEMPFILE + " " + "2>&1";

注意我已在您的指令结束处添加空格 2&gt;&amp; 1