📄 streams.examples.html
字号:
<html><!-- #BeginTemplate "/Templates/tmpl.dwt" --><head><!-- #BeginEditable "doctitle" --> <title>PTypes: streams: examples</title><!-- #EndEditable --> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link rel="stylesheet" href="styles.css"></head><body bgcolor="#FFFFFF" leftmargin="40" marginwidth="40"><p><a href="../index.html"><img src="title-1.7.gif" width="213" height="34" alt="C++ Portable Types Library (PTypes) Version 1.7" border="0"></a> <hr noshade><!-- #BeginEditable "body" --> <p class="hpath"><a href="index.html">Top</a>: <a href="streams.html">Streams</a>: Examples </p><p><b>Example 1.</b> This simple program creates a new file and writes a string to it.</p><blockquote> <pre>#include <pstreams.h>USING_PTYPESint main() { <span class="comment">// the outfile object is declared and constructed outside // the try...catch clause, since the exception object // contains a reference to the stream that caused the error. // besides, stream constructors and destructors in PTypes // never throw exceptions.</span> outfile f(fname); f.set_bufsize(1024); <span class="comment">// the default value in this version is 8192</span> try { f.open(); f.put("This is a test file."); f.close(); } catch (estream* e) { perr.putf("File error: %s\n", e->get_message()); delete e; } return 0;}</pre></blockquote><p><b>Example 2.</b> This program reads a C source, extracts identifiers and builds a usage dictionary. It does not understand C comments and strings though, but can be easily improved to understand them too.</p><blockquote> <pre>#include <ptypes.h>#include <pstreams.h>USING_PTYPESconst cset letters("_A-Za-z");const cset digits("0-9");const cset identchars = letters + digits;const cset otherchars = !letters;void main(int argc, char* argv[]) { strlist dic(SL_SORTED); infile f(argv[1]); try { f.open(); while (!f.get_eof()) { char c = f.preview(); <span class="comment">// a C identifier begins with a letter</span> if (c & letters) { <span class="comment">// ... and may contain letters and digits</span> string ident = f.token(identchars); int i; if (!search(dic, ident, i)) add(dic, ident, 0); } else <span class="comment">// ignore everything else</span> f.skiptoken(otherchars); } } catch (estream* e) { pout.putf("Error: %s\n", e->get_message()); delete e; } <span class="comment">// now print the dictionary</span> for (int i = 0; i < length(dic); i++) pout.putline(getstr(dic, i));}</pre></blockquote><p class="seealso">See also: <a href="streams.iobase.html">iobase</a>, <a href="streams.instm.html">instm</a>, <a href="streams.infile.html">infile</a>, <a href="streams.outstm.html">outstm</a>, <a href="streams.outfile.html">outfile</a>, <a href="streams.errors.html">Error handling</a></p><!-- #EndEditable --><hr size="1"><a href="../index.html" class="ns">PTypes home</a></body><!-- #EndTemplate --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -