c语言对命令行字符串分割,->>>关于命令行字符串的分离及重定向,该如何处理

当前位置:我的异常网» C语言 » ->>>关于命令行字符串的分离及重定向,该如

->>>关于命令行字符串的分离及重定向,该如何处理

www.myexceptions.net  网友分享于:2013-02-21  浏览:17次

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

问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:   写输出内容重定向的功能的思路?

------解决方案--------------------

有现成函数getopt()

------解决方案--------------------

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

#include

using namespace std;

void redirect(ostream&);

int main()

{

cout < < "the first row " < < endl;

redirect (cout);

cout < < "the last row " < < endl;

文章评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值