为什么下面的系统调用outut不会重定向到文件

why below system call ouput is not redirect to a file ?

本文关键字:重定向 文件 outut 系统调用 为什么      更新时间:2023-10-16

嗨,我正在使用smbclient在我的C 代码中连接远程服务器,并查找在远程连接的服务器上是否启用写入权限,我将我重定向到一个名为tempfile的文件,如下:

命令是

smbClient//服务器/文件夹-U用户%pass -directory = subfolder -c'put file.txt'> tempfile

此命令的输出是

将file.txt放置为 subfolder tempfile.txt(2.0 kb/s)(平均2.0 kb/s)

如果字符串"放置文件" 在重定向的文件 tempfile 中存在strong>。但是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;
}

使用 2>&amp; 1

重定向stderr和stdout and stdout

更改您的命令如下

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

注意,我添加了 space 2>&amp; 1 在您的命令末端。