为什么下面的系统调用outut不会重定向到文件
why below system call ouput is not redirect to a file ?
嗨,我正在使用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 在您的命令末端。
相关文章:
- 获取通过重定向传入的输入文件的名称?
- 写入文件 a.txt,而终端重定向到文件 b.txt
- 将 clangd stderr 重定向到文件而不是控制台
- 是否可以将子进程的 stdout 重定向到父进程中的另一个文件?
- 重定向输入和输出时如何处理主文件I/O?
- 使用重定向命令从 stdin 读入的字符串"<"输入文件未正确附加
- 可以将Boost消息队列文件重定向到用户指定的位置
- 如何使用PHP将上传到本地主机的一个html文件重定向到磁盘上的另一个html文档
- 尝试将 std::cerr 重定向到文件时出现访问冲突异常
- 重定向到文件的C ++ COUT比使用Ofstream慢
- 将 Tcl 中的命令输出重定向到文件时,文件以 DOS 格式生成
- 将 c++ 程序语法错误重定向到窗口中的文件
- 如何在没有任何缓冲区的情况下将 stderr 重定向到文件?
- 将 ffmpeg 控制台输出重定向到C++中的字符串或文件
- Win32 GUI C(++) 应用将标准输出和标准输出重定向到磁盘上的同一文件
- 即使可执行文件的输入是从文件重定向的,如何强制从终端输入?
- C++输入重定向导致文件参数工作时出错
- C++ vector<pair<int,int>> std::all_of() 函数问题(重定向到头文件)
- 重定向文件访问
- Boost.IOStreams:如何使用"rdbuf"正确重定向文件流?