-------------->>>关于命令行字符串的分离及重定向

ngucc 2007-06-25 08:56:44

问1:
从键盘输入一字串string, 写一函数getstr()分离出命令和路径

具体说:去除开头空格.中间空格保留一个,并以中间这个空格(也可以是'>')为分界,空格前的放入str1,空格后的放入出str2.末尾的不管它.返回str1,str2

如" cd c:\ "处理成"cd c:\" 并将"cd"放入字串str1,"c:\"放入str2;分离后的串作为形参返到主函数中.
又如:
"dir>c:\xx.txt" str1为"dir" str2为"c:\xx.txt" *****
"notepad.exe " str1为空str2为"notepad.exe"
"c:\123.exe" str1为空,str2为"c:\123.exe"

我写的程序出现的问题是:对str2处理不尽人意,返回主函数中是要么没有,要么乱码.


问2: 写输出内容重定向的功能的思路?
...全文
302 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ngucc 2007-06-27
  • 打赏
  • 举报
回复
谢谢..我知道怎么做了.
:)
jixingzhong 2007-06-26
  • 打赏
  • 举报
回复
#include <sstream>
#include <string>
#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char* argv[])
{
string line;
string command, str1, str2;

cout<<"Input a string: ";
getline(cin, line); //输入 cd c:\ ,注意只接收两个独立串,即一个命令,一个参数
// 如果是多个参数, 类似处理即可
istringstream t(line);
t>>str1>>str2; //t>>str1>>str2>>str3 ... 处理多个参数
command = str1+' '+str2;

cout<<"command = "<<command<<endl
<<"str1 = "<<str1<<endl
<<"str2 = "<<str2<<endl;

system("pause");
return 0;
}
jixingzhong 2007-06-26
  • 打赏
  • 举报
回复
The C++ Standard Library

13.10.3 Redirecting Standard Streams
In the old implementation of the IOStream library, the global streams cin, cout, cerr, and
clog were objects of the classes istream_withassign and ostream_withassign. It was
therefore possible to redirect the streams by assigning streams to other streams. This possibility
was removed from the C++ standard library. However, the possibility to redirect streams was
retained and extended to apply to all streams. A stream can be redirected by setting a stream
buffer.
The setting of stream buffers means the redirection of I/O streams controlled by the program
without help from the operating system. For example, the following statements set things up such
that output written to cout is not sent to the standard output channel but rather to the file
cout.txt:
std::ofstream file ("cout.txt");
std::cout.rdbuf (file.rdbuf());
The function copyfmt() can be used to assign all format information of a given stream to
another stream object:
std::ofstream file ("cout.txt");
file.copyfmt (std::cout);
std::cout.rdbuf (file.rdbuf());
Caution! The object file is local and is destroyed at the end of the block. This also destroys the
corresponding stream buffer. This differs from the "normal" streams because file streams allocate
their stream buffer objects at construction time and destroy them on destruction. Thus, in this
example, cout can no longer be used for writing. Actually, it cannot even be destroyed safely at
program termination. Thus, the old buffer should always be saved and restored later! The
following example does this in the function redirect():
// io/redirect.cpp
#include <iostream>
#include <fstream>
using namespace std;
void redirect(ostream&);
int main()
{
cout << "the first row" << endl;
redirect (cout);
cout << "the last row" << endl;
}
void redirect (ostream& strm)
{
ofstream file("redirect.txt");
// save output buffer of the stream
streambuf* strm_buffer = strm.rdbuf();
// redirect ouput into the file
strm.rdbuf (file.rdbuf());
file << "one row for the file" << endl;
strm << "one row for the stream" << endl;
// restore old output buffer
strm.rdbuf (strm_buffer);
} // closes file AND its buffer automatically
The output of the program is this
the first row
the last row
and the contents of the file redirect.txt are
one row for the file
one row for the stream
As you can see, the output written in redirect() to cout (using the parameter name strm) is
sent to the file. The output written after the execution of redirect() in main() is sent to the
restored output channel.
cceczjxy 2007-06-26
  • 打赏
  • 举报
回复
有现成函数getopt()
ngucc 2007-06-25
  • 打赏
  • 举报
回复
除了自已写,有现成了函数吗?

关于问2: dir>c:\dir_content.txt
把当前dir的内容保存在c:\dir_content.txt里.

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧