0


C++ :输入/输出流

输入输出流

  • I/O (输入/输出) 流类库提供对象之间的交互服务。
  • 流类库预定义了一批流对象,连接常用的外部设备。
  • 使用者可以定义所需的I/O流对象,使用流库提供的工作方式实现数据传输。

一:流类和流对象

  1. 在程序中,对数据的输入/输出是以字节流实现的。
  2. 应用程序对字节序列做出各种数据解释。
  3. I/O 系统的任务就是在内存和外部设备之间稳定可靠的传输数据和解释数据。
  4. 流对象可以建立和删除,可以从流中获取数据,也可以向流中添加数据。

流类库

  • 流库(stream library)使用继承方法建立的输入输出类库。
  • 流库具有两个平行的基类:streambuf 和 ios 类,所有流类均以两者之一作为基类。
  • streambuf 提供了对缓冲区的低级操作:设置缓冲区 ; 对缓冲区指针操作 ; 向缓冲区存/取字符。
  • ios 类及其派生类提供用户使用流类的接口,支持对 streambuf 的缓冲区输入/输出的格式化或非格式化转换。

streambuf 对缓冲区的低级操作:

#mermaid-svg-PWsw6tLGqp0nxhEZ .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-PWsw6tLGqp0nxhEZ .label text{fill:#333}#mermaid-svg-PWsw6tLGqp0nxhEZ .node rect,#mermaid-svg-PWsw6tLGqp0nxhEZ .node circle,#mermaid-svg-PWsw6tLGqp0nxhEZ .node ellipse,#mermaid-svg-PWsw6tLGqp0nxhEZ .node polygon,#mermaid-svg-PWsw6tLGqp0nxhEZ .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-PWsw6tLGqp0nxhEZ .node .label{text-align:center;fill:#333}#mermaid-svg-PWsw6tLGqp0nxhEZ .node.clickable{cursor:pointer}#mermaid-svg-PWsw6tLGqp0nxhEZ .arrowheadPath{fill:#333}#mermaid-svg-PWsw6tLGqp0nxhEZ .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-PWsw6tLGqp0nxhEZ .flowchart-link{stroke:#333;fill:none}#mermaid-svg-PWsw6tLGqp0nxhEZ .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-PWsw6tLGqp0nxhEZ .edgeLabel rect{opacity:0.9}#mermaid-svg-PWsw6tLGqp0nxhEZ .edgeLabel span{color:#333}#mermaid-svg-PWsw6tLGqp0nxhEZ .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-PWsw6tLGqp0nxhEZ .cluster text{fill:#333}#mermaid-svg-PWsw6tLGqp0nxhEZ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-PWsw6tLGqp0nxhEZ .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-PWsw6tLGqp0nxhEZ text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-PWsw6tLGqp0nxhEZ .actor-line{stroke:grey}#mermaid-svg-PWsw6tLGqp0nxhEZ .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-PWsw6tLGqp0nxhEZ .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-PWsw6tLGqp0nxhEZ #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-PWsw6tLGqp0nxhEZ .sequenceNumber{fill:#fff}#mermaid-svg-PWsw6tLGqp0nxhEZ #sequencenumber{fill:#333}#mermaid-svg-PWsw6tLGqp0nxhEZ #crosshead path{fill:#333;stroke:#333}#mermaid-svg-PWsw6tLGqp0nxhEZ .messageText{fill:#333;stroke:#333}#mermaid-svg-PWsw6tLGqp0nxhEZ .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-PWsw6tLGqp0nxhEZ .labelText,#mermaid-svg-PWsw6tLGqp0nxhEZ .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-PWsw6tLGqp0nxhEZ .loopText,#mermaid-svg-PWsw6tLGqp0nxhEZ .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-PWsw6tLGqp0nxhEZ .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-PWsw6tLGqp0nxhEZ .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-PWsw6tLGqp0nxhEZ .noteText,#mermaid-svg-PWsw6tLGqp0nxhEZ .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-PWsw6tLGqp0nxhEZ .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-PWsw6tLGqp0nxhEZ .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-PWsw6tLGqp0nxhEZ .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-PWsw6tLGqp0nxhEZ .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PWsw6tLGqp0nxhEZ .section{stroke:none;opacity:0.2}#mermaid-svg-PWsw6tLGqp0nxhEZ .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-PWsw6tLGqp0nxhEZ .section2{fill:#fff400}#mermaid-svg-PWsw6tLGqp0nxhEZ .section1,#mermaid-svg-PWsw6tLGqp0nxhEZ .section3{fill:#fff;opacity:0.2}#mermaid-svg-PWsw6tLGqp0nxhEZ .sectionTitle0{fill:#333}#mermaid-svg-PWsw6tLGqp0nxhEZ .sectionTitle1{fill:#333}#mermaid-svg-PWsw6tLGqp0nxhEZ .sectionTitle2{fill:#333}#mermaid-svg-PWsw6tLGqp0nxhEZ .sectionTitle3{fill:#333}#mermaid-svg-PWsw6tLGqp0nxhEZ .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PWsw6tLGqp0nxhEZ .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-PWsw6tLGqp0nxhEZ .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PWsw6tLGqp0nxhEZ .grid path{stroke-width:0}#mermaid-svg-PWsw6tLGqp0nxhEZ .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-PWsw6tLGqp0nxhEZ .task{stroke-width:2}#mermaid-svg-PWsw6tLGqp0nxhEZ .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PWsw6tLGqp0nxhEZ .taskText:not([font-size]){font-size:11px}#mermaid-svg-PWsw6tLGqp0nxhEZ .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PWsw6tLGqp0nxhEZ .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-PWsw6tLGqp0nxhEZ .task.clickable{cursor:pointer}#mermaid-svg-PWsw6tLGqp0nxhEZ .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-PWsw6tLGqp0nxhEZ .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-PWsw6tLGqp0nxhEZ .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-PWsw6tLGqp0nxhEZ .taskText0,#mermaid-svg-PWsw6tLGqp0nxhEZ .taskText1,#mermaid-svg-PWsw6tLGqp0nxhEZ .taskText2,#mermaid-svg-PWsw6tLGqp0nxhEZ .taskText3{fill:#fff}#mermaid-svg-PWsw6tLGqp0nxhEZ .task0,#mermaid-svg-PWsw6tLGqp0nxhEZ .task1,#mermaid-svg-PWsw6tLGqp0nxhEZ .task2,#mermaid-svg-PWsw6tLGqp0nxhEZ .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-PWsw6tLGqp0nxhEZ .taskTextOutside0,#mermaid-svg-PWsw6tLGqp0nxhEZ .taskTextOutside2{fill:#000}#mermaid-svg-PWsw6tLGqp0nxhEZ .taskTextOutside1,#mermaid-svg-PWsw6tLGqp0nxhEZ .taskTextOutside3{fill:#000}#mermaid-svg-PWsw6tLGqp0nxhEZ .active0,#mermaid-svg-PWsw6tLGqp0nxhEZ .active1,#mermaid-svg-PWsw6tLGqp0nxhEZ .active2,#mermaid-svg-PWsw6tLGqp0nxhEZ .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-PWsw6tLGqp0nxhEZ .activeText0,#mermaid-svg-PWsw6tLGqp0nxhEZ .activeText1,#mermaid-svg-PWsw6tLGqp0nxhEZ .activeText2,#mermaid-svg-PWsw6tLGqp0nxhEZ .activeText3{fill:#000 !important}#mermaid-svg-PWsw6tLGqp0nxhEZ .done0,#mermaid-svg-PWsw6tLGqp0nxhEZ .done1,#mermaid-svg-PWsw6tLGqp0nxhEZ .done2,#mermaid-svg-PWsw6tLGqp0nxhEZ .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-PWsw6tLGqp0nxhEZ .doneText0,#mermaid-svg-PWsw6tLGqp0nxhEZ .doneText1,#mermaid-svg-PWsw6tLGqp0nxhEZ .doneText2,#mermaid-svg-PWsw6tLGqp0nxhEZ .doneText3{fill:#000 !important}#mermaid-svg-PWsw6tLGqp0nxhEZ .crit0,#mermaid-svg-PWsw6tLGqp0nxhEZ .crit1,#mermaid-svg-PWsw6tLGqp0nxhEZ .crit2,#mermaid-svg-PWsw6tLGqp0nxhEZ .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-PWsw6tLGqp0nxhEZ .activeCrit0,#mermaid-svg-PWsw6tLGqp0nxhEZ .activeCrit1,#mermaid-svg-PWsw6tLGqp0nxhEZ .activeCrit2,#mermaid-svg-PWsw6tLGqp0nxhEZ .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-PWsw6tLGqp0nxhEZ .doneCrit0,#mermaid-svg-PWsw6tLGqp0nxhEZ .doneCrit1,#mermaid-svg-PWsw6tLGqp0nxhEZ .doneCrit2,#mermaid-svg-PWsw6tLGqp0nxhEZ .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-PWsw6tLGqp0nxhEZ .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-PWsw6tLGqp0nxhEZ .milestoneText{font-style:italic}#mermaid-svg-PWsw6tLGqp0nxhEZ .doneCritText0,#mermaid-svg-PWsw6tLGqp0nxhEZ .doneCritText1,#mermaid-svg-PWsw6tLGqp0nxhEZ .doneCritText2,#mermaid-svg-PWsw6tLGqp0nxhEZ .doneCritText3{fill:#000 !important}#mermaid-svg-PWsw6tLGqp0nxhEZ .activeCritText0,#mermaid-svg-PWsw6tLGqp0nxhEZ .activeCritText1,#mermaid-svg-PWsw6tLGqp0nxhEZ .activeCritText2,#mermaid-svg-PWsw6tLGqp0nxhEZ .activeCritText3{fill:#000 !important}#mermaid-svg-PWsw6tLGqp0nxhEZ .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PWsw6tLGqp0nxhEZ g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-PWsw6tLGqp0nxhEZ g.classGroup text .title{font-weight:bolder}#mermaid-svg-PWsw6tLGqp0nxhEZ g.clickable{cursor:pointer}#mermaid-svg-PWsw6tLGqp0nxhEZ g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-PWsw6tLGqp0nxhEZ g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-PWsw6tLGqp0nxhEZ .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-PWsw6tLGqp0nxhEZ .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-PWsw6tLGqp0nxhEZ .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-PWsw6tLGqp0nxhEZ .dashed-line{stroke-dasharray:3}#mermaid-svg-PWsw6tLGqp0nxhEZ #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-PWsw6tLGqp0nxhEZ #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-PWsw6tLGqp0nxhEZ #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-PWsw6tLGqp0nxhEZ #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-PWsw6tLGqp0nxhEZ #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-PWsw6tLGqp0nxhEZ #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-PWsw6tLGqp0nxhEZ #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-PWsw6tLGqp0nxhEZ #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-PWsw6tLGqp0nxhEZ .commit-id,#mermaid-svg-PWsw6tLGqp0nxhEZ .commit-msg,#mermaid-svg-PWsw6tLGqp0nxhEZ .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PWsw6tLGqp0nxhEZ .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PWsw6tLGqp0nxhEZ .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PWsw6tLGqp0nxhEZ g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PWsw6tLGqp0nxhEZ g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-PWsw6tLGqp0nxhEZ g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-PWsw6tLGqp0nxhEZ g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-PWsw6tLGqp0nxhEZ g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-PWsw6tLGqp0nxhEZ g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-PWsw6tLGqp0nxhEZ .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-PWsw6tLGqp0nxhEZ .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-PWsw6tLGqp0nxhEZ .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-PWsw6tLGqp0nxhEZ .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-PWsw6tLGqp0nxhEZ .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-PWsw6tLGqp0nxhEZ .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-PWsw6tLGqp0nxhEZ .edgeLabel text{fill:#333}#mermaid-svg-PWsw6tLGqp0nxhEZ .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PWsw6tLGqp0nxhEZ .node circle.state-start{fill:black;stroke:black}#mermaid-svg-PWsw6tLGqp0nxhEZ .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-PWsw6tLGqp0nxhEZ #statediagram-barbEnd{fill:#9370db}#mermaid-svg-PWsw6tLGqp0nxhEZ .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-PWsw6tLGqp0nxhEZ .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-PWsw6tLGqp0nxhEZ .statediagram-state .divider{stroke:#9370db}#mermaid-svg-PWsw6tLGqp0nxhEZ .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-PWsw6tLGqp0nxhEZ .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-PWsw6tLGqp0nxhEZ .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-PWsw6tLGqp0nxhEZ .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-PWsw6tLGqp0nxhEZ .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-PWsw6tLGqp0nxhEZ .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-PWsw6tLGqp0nxhEZ .note-edge{stroke-dasharray:5}#mermaid-svg-PWsw6tLGqp0nxhEZ .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-PWsw6tLGqp0nxhEZ .error-icon{fill:#522}#mermaid-svg-PWsw6tLGqp0nxhEZ .error-text{fill:#522;stroke:#522}#mermaid-svg-PWsw6tLGqp0nxhEZ .edge-thickness-normal{stroke-width:2px}#mermaid-svg-PWsw6tLGqp0nxhEZ .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-PWsw6tLGqp0nxhEZ .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-PWsw6tLGqp0nxhEZ .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-PWsw6tLGqp0nxhEZ .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-PWsw6tLGqp0nxhEZ .marker{fill:#333}#mermaid-svg-PWsw6tLGqp0nxhEZ .marker.cross{stroke:#333}

:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-PWsw6tLGqp0nxhEZ {
color: rgba(0, 0, 0, 0.75);
font: ;
}
文件缓冲区管理

字符串缓冲区管理

标准I/O缓冲区管理

       streambuf 
     

       filebuf 
     

       strstreambuf 
     

       stdiobuf 
     

ios 提供的用户接口:

#mermaid-svg-PLSBBrJuC8cfrpJi .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-PLSBBrJuC8cfrpJi .label text{fill:#333}#mermaid-svg-PLSBBrJuC8cfrpJi .node rect,#mermaid-svg-PLSBBrJuC8cfrpJi .node circle,#mermaid-svg-PLSBBrJuC8cfrpJi .node ellipse,#mermaid-svg-PLSBBrJuC8cfrpJi .node polygon,#mermaid-svg-PLSBBrJuC8cfrpJi .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-PLSBBrJuC8cfrpJi .node .label{text-align:center;fill:#333}#mermaid-svg-PLSBBrJuC8cfrpJi .node.clickable{cursor:pointer}#mermaid-svg-PLSBBrJuC8cfrpJi .arrowheadPath{fill:#333}#mermaid-svg-PLSBBrJuC8cfrpJi .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-PLSBBrJuC8cfrpJi .flowchart-link{stroke:#333;fill:none}#mermaid-svg-PLSBBrJuC8cfrpJi .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-PLSBBrJuC8cfrpJi .edgeLabel rect{opacity:0.9}#mermaid-svg-PLSBBrJuC8cfrpJi .edgeLabel span{color:#333}#mermaid-svg-PLSBBrJuC8cfrpJi .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-PLSBBrJuC8cfrpJi .cluster text{fill:#333}#mermaid-svg-PLSBBrJuC8cfrpJi div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-PLSBBrJuC8cfrpJi .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-PLSBBrJuC8cfrpJi text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-PLSBBrJuC8cfrpJi .actor-line{stroke:grey}#mermaid-svg-PLSBBrJuC8cfrpJi .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-PLSBBrJuC8cfrpJi .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-PLSBBrJuC8cfrpJi #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-PLSBBrJuC8cfrpJi .sequenceNumber{fill:#fff}#mermaid-svg-PLSBBrJuC8cfrpJi #sequencenumber{fill:#333}#mermaid-svg-PLSBBrJuC8cfrpJi #crosshead path{fill:#333;stroke:#333}#mermaid-svg-PLSBBrJuC8cfrpJi .messageText{fill:#333;stroke:#333}#mermaid-svg-PLSBBrJuC8cfrpJi .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-PLSBBrJuC8cfrpJi .labelText,#mermaid-svg-PLSBBrJuC8cfrpJi .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-PLSBBrJuC8cfrpJi .loopText,#mermaid-svg-PLSBBrJuC8cfrpJi .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-PLSBBrJuC8cfrpJi .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-PLSBBrJuC8cfrpJi .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-PLSBBrJuC8cfrpJi .noteText,#mermaid-svg-PLSBBrJuC8cfrpJi .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-PLSBBrJuC8cfrpJi .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-PLSBBrJuC8cfrpJi .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-PLSBBrJuC8cfrpJi .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-PLSBBrJuC8cfrpJi .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PLSBBrJuC8cfrpJi .section{stroke:none;opacity:0.2}#mermaid-svg-PLSBBrJuC8cfrpJi .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-PLSBBrJuC8cfrpJi .section2{fill:#fff400}#mermaid-svg-PLSBBrJuC8cfrpJi .section1,#mermaid-svg-PLSBBrJuC8cfrpJi .section3{fill:#fff;opacity:0.2}#mermaid-svg-PLSBBrJuC8cfrpJi .sectionTitle0{fill:#333}#mermaid-svg-PLSBBrJuC8cfrpJi .sectionTitle1{fill:#333}#mermaid-svg-PLSBBrJuC8cfrpJi .sectionTitle2{fill:#333}#mermaid-svg-PLSBBrJuC8cfrpJi .sectionTitle3{fill:#333}#mermaid-svg-PLSBBrJuC8cfrpJi .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PLSBBrJuC8cfrpJi .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-PLSBBrJuC8cfrpJi .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PLSBBrJuC8cfrpJi .grid path{stroke-width:0}#mermaid-svg-PLSBBrJuC8cfrpJi .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-PLSBBrJuC8cfrpJi .task{stroke-width:2}#mermaid-svg-PLSBBrJuC8cfrpJi .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PLSBBrJuC8cfrpJi .taskText:not([font-size]){font-size:11px}#mermaid-svg-PLSBBrJuC8cfrpJi .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PLSBBrJuC8cfrpJi .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-PLSBBrJuC8cfrpJi .task.clickable{cursor:pointer}#mermaid-svg-PLSBBrJuC8cfrpJi .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-PLSBBrJuC8cfrpJi .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-PLSBBrJuC8cfrpJi .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-PLSBBrJuC8cfrpJi .taskText0,#mermaid-svg-PLSBBrJuC8cfrpJi .taskText1,#mermaid-svg-PLSBBrJuC8cfrpJi .taskText2,#mermaid-svg-PLSBBrJuC8cfrpJi .taskText3{fill:#fff}#mermaid-svg-PLSBBrJuC8cfrpJi .task0,#mermaid-svg-PLSBBrJuC8cfrpJi .task1,#mermaid-svg-PLSBBrJuC8cfrpJi .task2,#mermaid-svg-PLSBBrJuC8cfrpJi .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-PLSBBrJuC8cfrpJi .taskTextOutside0,#mermaid-svg-PLSBBrJuC8cfrpJi .taskTextOutside2{fill:#000}#mermaid-svg-PLSBBrJuC8cfrpJi .taskTextOutside1,#mermaid-svg-PLSBBrJuC8cfrpJi .taskTextOutside3{fill:#000}#mermaid-svg-PLSBBrJuC8cfrpJi .active0,#mermaid-svg-PLSBBrJuC8cfrpJi .active1,#mermaid-svg-PLSBBrJuC8cfrpJi .active2,#mermaid-svg-PLSBBrJuC8cfrpJi .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-PLSBBrJuC8cfrpJi .activeText0,#mermaid-svg-PLSBBrJuC8cfrpJi .activeText1,#mermaid-svg-PLSBBrJuC8cfrpJi .activeText2,#mermaid-svg-PLSBBrJuC8cfrpJi .activeText3{fill:#000 !important}#mermaid-svg-PLSBBrJuC8cfrpJi .done0,#mermaid-svg-PLSBBrJuC8cfrpJi .done1,#mermaid-svg-PLSBBrJuC8cfrpJi .done2,#mermaid-svg-PLSBBrJuC8cfrpJi .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-PLSBBrJuC8cfrpJi .doneText0,#mermaid-svg-PLSBBrJuC8cfrpJi .doneText1,#mermaid-svg-PLSBBrJuC8cfrpJi .doneText2,#mermaid-svg-PLSBBrJuC8cfrpJi .doneText3{fill:#000 !important}#mermaid-svg-PLSBBrJuC8cfrpJi .crit0,#mermaid-svg-PLSBBrJuC8cfrpJi .crit1,#mermaid-svg-PLSBBrJuC8cfrpJi .crit2,#mermaid-svg-PLSBBrJuC8cfrpJi .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-PLSBBrJuC8cfrpJi .activeCrit0,#mermaid-svg-PLSBBrJuC8cfrpJi .activeCrit1,#mermaid-svg-PLSBBrJuC8cfrpJi .activeCrit2,#mermaid-svg-PLSBBrJuC8cfrpJi .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-PLSBBrJuC8cfrpJi .doneCrit0,#mermaid-svg-PLSBBrJuC8cfrpJi .doneCrit1,#mermaid-svg-PLSBBrJuC8cfrpJi .doneCrit2,#mermaid-svg-PLSBBrJuC8cfrpJi .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-PLSBBrJuC8cfrpJi .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-PLSBBrJuC8cfrpJi .milestoneText{font-style:italic}#mermaid-svg-PLSBBrJuC8cfrpJi .doneCritText0,#mermaid-svg-PLSBBrJuC8cfrpJi .doneCritText1,#mermaid-svg-PLSBBrJuC8cfrpJi .doneCritText2,#mermaid-svg-PLSBBrJuC8cfrpJi .doneCritText3{fill:#000 !important}#mermaid-svg-PLSBBrJuC8cfrpJi .activeCritText0,#mermaid-svg-PLSBBrJuC8cfrpJi .activeCritText1,#mermaid-svg-PLSBBrJuC8cfrpJi .activeCritText2,#mermaid-svg-PLSBBrJuC8cfrpJi .activeCritText3{fill:#000 !important}#mermaid-svg-PLSBBrJuC8cfrpJi .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PLSBBrJuC8cfrpJi g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-PLSBBrJuC8cfrpJi g.classGroup text .title{font-weight:bolder}#mermaid-svg-PLSBBrJuC8cfrpJi g.clickable{cursor:pointer}#mermaid-svg-PLSBBrJuC8cfrpJi g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-PLSBBrJuC8cfrpJi g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-PLSBBrJuC8cfrpJi .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-PLSBBrJuC8cfrpJi .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-PLSBBrJuC8cfrpJi .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-PLSBBrJuC8cfrpJi .dashed-line{stroke-dasharray:3}#mermaid-svg-PLSBBrJuC8cfrpJi #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-PLSBBrJuC8cfrpJi #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-PLSBBrJuC8cfrpJi #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-PLSBBrJuC8cfrpJi #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-PLSBBrJuC8cfrpJi #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-PLSBBrJuC8cfrpJi #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-PLSBBrJuC8cfrpJi #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-PLSBBrJuC8cfrpJi #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-PLSBBrJuC8cfrpJi .commit-id,#mermaid-svg-PLSBBrJuC8cfrpJi .commit-msg,#mermaid-svg-PLSBBrJuC8cfrpJi .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PLSBBrJuC8cfrpJi .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PLSBBrJuC8cfrpJi .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PLSBBrJuC8cfrpJi g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PLSBBrJuC8cfrpJi g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-PLSBBrJuC8cfrpJi g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-PLSBBrJuC8cfrpJi g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-PLSBBrJuC8cfrpJi g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-PLSBBrJuC8cfrpJi g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-PLSBBrJuC8cfrpJi .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-PLSBBrJuC8cfrpJi .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-PLSBBrJuC8cfrpJi .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-PLSBBrJuC8cfrpJi .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-PLSBBrJuC8cfrpJi .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-PLSBBrJuC8cfrpJi .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-PLSBBrJuC8cfrpJi .edgeLabel text{fill:#333}#mermaid-svg-PLSBBrJuC8cfrpJi .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-PLSBBrJuC8cfrpJi .node circle.state-start{fill:black;stroke:black}#mermaid-svg-PLSBBrJuC8cfrpJi .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-PLSBBrJuC8cfrpJi #statediagram-barbEnd{fill:#9370db}#mermaid-svg-PLSBBrJuC8cfrpJi .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-PLSBBrJuC8cfrpJi .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-PLSBBrJuC8cfrpJi .statediagram-state .divider{stroke:#9370db}#mermaid-svg-PLSBBrJuC8cfrpJi .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-PLSBBrJuC8cfrpJi .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-PLSBBrJuC8cfrpJi .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-PLSBBrJuC8cfrpJi .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-PLSBBrJuC8cfrpJi .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-PLSBBrJuC8cfrpJi .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-PLSBBrJuC8cfrpJi .note-edge{stroke-dasharray:5}#mermaid-svg-PLSBBrJuC8cfrpJi .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-PLSBBrJuC8cfrpJi .error-icon{fill:#522}#mermaid-svg-PLSBBrJuC8cfrpJi .error-text{fill:#522;stroke:#522}#mermaid-svg-PLSBBrJuC8cfrpJi .edge-thickness-normal{stroke-width:2px}#mermaid-svg-PLSBBrJuC8cfrpJi .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-PLSBBrJuC8cfrpJi .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-PLSBBrJuC8cfrpJi .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-PLSBBrJuC8cfrpJi .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-PLSBBrJuC8cfrpJi .marker{fill:#333}#mermaid-svg-PLSBBrJuC8cfrpJi .marker.cross{stroke:#333}

:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-PLSBBrJuC8cfrpJi {
color: rgba(0, 0, 0, 0.75);
font: ;
}
输入流

输出流

输入文件流

输入串流

重载运算符 = 号

输入输出流

输出文件流

输出串流

重载运算符 = 号

输入输出流

输入/输出文件流

输入/输出串流

输入/输出标准流

       ios 
     

       istream 
     

       ostream 
     

       ifstream 
     

       istrstream 
     

       istream_withassign 
     

       iostream 
     

       ofstream 
     

       ostrstream 
     

       ostream_withassign 
     

       fstream 
     

       strstream 
     

       stdiostream 
     

头文件

  • iostream 包含所有输入输出流所需的基本信息 含有 cin , cout, cerr, clog 对象,提供无格式和格式化的I/O。
  • iomanip 包含格式化I/O 操纵算子,用于指定数据输入输出的格式。
  • fstream 处理文件信息,包括建立文件,读写文件的各种操作接口。

每种C++版本还包含其他一些与I/O 相关的库,提供特定系统的某些功能。

二:标准流和流操作

  • 标准流是C++ 预定义的对象,提供内存与外部设备进行数据交互功能。
  • 流的操作是流类的共有成员函数。

在这里插入图片描述标准流为用户常用的外部设备提供与内存之间的通信通道,对数据进行解释和传输,提供必要的数据缓冲。

标准流

  1. cin istream 类的对象,通常连接键盘,可以重定向。
  2. cout ostream 类的对象,通常连向显示器,可以重定向。
  3. cerr ostream 类的对象,连向显示器,不可以重定向。
  4. clog ostream 类的对象,也用于向标准错误设备(默认设备为显示器)输出有关错误信息,不可以被重定向。但它可以被缓冲,当缓冲区被填满或遇 endl 时向显示器输出信息。

重定向的演示:

//重定向的演示:#include<iostream>//#include<iomanip>//#include<fstream>usingnamespace std;voidfun(int a,int b){if(b ==0){
        cerr <<"除数遇到0,这个错误消息不能被重定向。 \n";}else{
        cout << a <<'/'<< b <<'='<< a / b << endl;}}intmain(){
    std::cout <<"Hello World!\n";int n1, n2;
    cout <<"one:\n";
    cin >> n1 >> n2;fun(n1, n2);
    cerr <<"tow:\n";fun(10,0);}

输出演示
在控制台是可以输出 cerr 的信息。
下面利用VS的命令行进行对代码编译,执行,重定向输入和输出。新建一个 txt 文本,把代码拷贝进去,(代码中的中改为英文)更改后缀为.cpp
如下图:
在这里插入图片描述
打开开发人员命令行:

在这里插入图片描述
打开后切换到源代码目录,使用 cl 命令编译 .cpp 源文件:
在这里插入图片描述
执行编译后的 exe文件:在窗口中可以正常输出。
在这里插入图片描述
利用重定向符号 > (输出) 和 < (输入) 进行重定向到文件的测试:
新建两个文本文件,num作为输入,out作为输出。
在这里插入图片描述
在这里插入图片描述
测试结果:cerr 输出不能进行重定向。

输入/输出 流操作:

istream 类的公有成员函数:
函数功能read无格式输入指定字节数get从流中提取字符,包括空格getline从流中提取一行字符ignore提取并丢弃流中指定字符peek返回流中下一个字符,但不从流中删除gcount统计最后输入的字符个数eatwhite忽略前导空格seekg移动输入流指针tellg返回输入流中指定位置的指针operstor >>提取运算符
ostream 类的公有成员函数
函数功能put无格式插入一个字符write从无格式,插入一个字节序列flush刷新输出流seekp移动输出流指针tellp返回输出流中指定位置的指针operstor<<插入运算符
演示:

#include<iostream>#include<iomanip>//#include<fstream>usingnamespace std;//用get函数从键盘输入字符intmain(){{constchar* str ="12345";
        cout << str << endl;
        cout <<(void*)str << endl;int n =strlen(str);for(size_t i =0; i < n; i++){
            cout <<*((char*)((void*)str)+ i);}
        cout <<  endl;}{
        cout.put('h').put('e').put('l').put('l').put('o').put('\n');//等价于 C语言 putchar,但可以链式编程。char str[20]="Hello World!";
        cout.write(str,strlen(str)).put('\n').put('A').write(str+5,strlen(str+5)).put('\n');//write输出字符串,参数:首地址,长度;.可以链式编程。
        cout <<"\n\n\n";//格式化对齐字符,右对齐
        cout.width(10);
        cout <<"123456"<< endl;
       
        cout.width(20);
        cout <<"ABCDEFG"<< endl;

        cout <<"字段填充"<< endl;
        cout.width(20);
        cout.fill('*');
        cout <<"ABCDEFG"<< endl;//输出字符串与填充字符个数共20
        cout <<"\n\n\n";}{
        cout <<"设置浮点数输出精度:"<< endl;double d =1.123456789;
        cout << d << endl;
        cout.precision(9);
        cout << d << endl;float f =1.00;
        cout << f << endl;
        cout.precision(3);
        cout.setf(ios::showpoint);//设置显示小数点
        cout << f << endl;
        cout <<"\n\n\n";}{int num =256;
        cout <<"十进制:"<< dec << num << endl;
        cout <<"十六进制:"<< hex << num << endl;
        cout <<"八进制:"<< oct << num << endl;}return0;}

演示效果

#include<iostream>#include<iomanip>//#include<fstream>usingnamespace std;voidmain(){{char c = cin.get();//get函数 【有返回值且无参数时】不可链式。只接受一个字符。等价于 C getchar函数
        cout.put(c).put(':').put(c).put('\n');//put 函数可以链式编程char c1, c2, c3, c4;
        cin.get(c1).get(c2).get(c3).get(c4);
        cout.put(c1).put(':').put(c2).put(':').put(c3).put(':').put(c4).put('\n');}{char ch1[10]{0};char ch2[10]{0};
        cin.get(ch1,10);
        cout << ch1 << endl;
        cin.ignore();//忽略
        cin.get(ch2,10);
        cout << ch2 << endl;}

    cin.get();}
#include<iostream>#include<iomanip>//#include<fstream>usingnamespace std;voidmain(){char str1[20]{0};
    cin.getline(str1,20,'#');//读取一行数据, 制定 # 为结束符。
    cout << str1 << endl;char str2[20]{0};
    cin.getline(str2,20,'#');//读取一行数据, 制定 # 为结束符。
    cout << str2 << endl;

    cin.get();}

流的错误状态:
标识常量值含义goodbit0x00状态正常eofbit0x01文件结束符failbit0x02I/O 操作失败,数据未丢失,可以恢复badbit0x04非法操作,数据丢失,不可恢复

三: 串流

  • 串流类是 ios 中派生类。
  • C++ 的串流对象可以连接string对象或字符串。
  • 串流提取数据时对字符串按变量类型进行解释,插入数据时把类型数据转成字符串。
  • 串流I/O具有格式化功能。

演示:istringstream:从输入串流对象提取数据

#include<iostream>#include<sstream>#include<cstdlib>#include<string>usingnamespace std;structDataInfo{
    string s1;
    string s2;int num;char c;double db;voidshow(){
        cout <<"s1=  "<<this->s1 << endl;
        cout <<"s2=  "<<this->s2 << endl;
        cout <<"num= "<<this->num << endl;
        cout <<"c=   "<<this->c << endl;
        cout <<"db=  "<<this->db << endl;}};intmain(){
    std::cout <<"Hello World!\n";

    stringstream sstr;//字符串流
    sstr.put('A').put(':').put('B').put(':');
    sstr <<"你好!C++ "<<3.1415926<<':'<<100;
    cout << sstr.str()<< endl;//相当于 C语言 sprintf 函数功能。{
        string str("Google Microsoft  125 A 3.14");
        DataInfo info;
        istringstream iput(str);//建立输入流
        iput >> info.s1 >> info.s2 >> info.num >> info.c >> info.db;
        info.show();}
    cout <<"\n\n\n";{char str[]="Google,Microsoft,125,A,3.14";for(char* ptr = str;*ptr !='\0'; ptr++){if(*ptr ==','){*ptr =' ';}}

        DataInfo info;
        istringstream iput(str);//建立输入流
        iput >> info.s1 >> info.s2 >> info.num >> info.c >> info.db;
        info.show();}return0;}

在这里插入图片描述
演示:ostringstream:向输出串流对象插入数据

#include<iostream>#include<sstream>#include<cstdlib>#include<string>usingnamespace std;voidmain(){char str[100]{0};
    ostringstream osput(str,sizeof(str));
    osput <<"abcdefg"<<','<<12345<<','<<12.345<<','<<'A';
    cout << osput.str()<< endl;
    cout << str << endl;//str没有数据, 只是起到缓冲区作用system("pause");}

在这里插入图片描述
演示:ostrstream:向输出串流对象插入数据

#include<iostream>#include<sstream>#include<strstream>#include<cstdlib>#include<string>usingnamespace std;voidmain(){char buf[80]{0};
    ostrstream ossOutput(buf,sizeof(buf));double x, y;
    cout <<"Input X:";
    cin >> x;
    cout <<"Input Y:";
    cin >> y;//endl表示输出并换行,ends表示输出加一个空格,flush表示什么都不加,直接输出
    ossOutput << x <<"*"<< y <<"="<< x * y << ends;//输出到字符串

    cout << buf << endl;system("pause");}

在这里插入图片描述

四:文件处理

  • C++ 把文件当作无结构的字节流,编码方式:文本文件,二进制文件。存取方式:顺序文件,随机文件。
  • ifstream , ofstream , fstream 类,用于文件与内存之间的数据传输。

文件和流

文件可以映射到内存,当作内存数据使用文件指针进行操作。我们操作文件,往往都是这种方式来实现的。
在这里插入图片描述

打开文件:

包括建立文件流对象;与外部文件进行关联;指定文件的打开方式。
打开文件有两种方式:

  1. 首先建立流对象,然会调用 fstream::open() 函数连接外部文件: 流对象 对象名; 对象名. open(文件全名,方式);
  2. 调用流类带参数的构造函数,建立流对象的同时连接外部文件:流类 对象名 (文件全名,方式);

open() 函数原型:

voidopen(constchar* _Filename, ios_base::open_mode _Mode);voidopen(const string& _Str, ios_base::openmode _Mode = ios_base::in | ios_base::out,int _Prot = ios_base::_Default_open_prot);voidopen(constchar* _Filename, ios_base::openmode _Mode = ios_base::in | ios_base::out,int _Prot = ios_base::_Default_open_prot)

filebuf , ifstream , ofstream , fstream 的构造函数具有相同的参数和缺省值,文件流的构造函数和open() 函数用于打开文件,析构函数在文件流被删除之前关闭文件。
参数_Filenamemodeprot含义操作文件名打开文件方式打开文件属性和保护方式(应用少,一般用省却值)
model 参数文件打开方式:
标识常量值含义ios::in0x01读方式打开文件ios::out0x02写方式打开文件ios::ate0x04打开文件时,指针指向文件尾部ios::app0x08追加方式ios::trunc0x10删除文件现有内容ios::_Nocreate0x40如果文件不存在,则打开操作失败ios::_Noreplace0x80如果文件存在,则打开操作失败ios::binary0x20二进制方式打开,默认为文本方式
演示:

#include<iostream>#include<fstream>#include<cstdlib>usingnamespace std;intmain(){
    std::cout <<"Hello World!\n";constchar* filepath ="D:\\test.txt";{
        ofstream outfile;//创建输出文件流
        outfile.open(filepath);
        outfile <<"C++,使用ofstream类写入文件测试。";
        outfile.close();//切记,关闭生效}{char str[256]{0};
        ifstream infile;//创建文件输入流
        infile.open(filepath);
        infile >> str;//如果字符串有空格会被截断
        infile.close();
        cout << str << endl;}system(filepath);//打开创建的文件}

在这里插入图片描述

#include<iostream>#include<fstream>#include<cstdlib>usingnamespace std;voidmain(){constchar* filepath ="D:\\test.txt";char str[256]{0};
    ifstream infile;//创建文件输入流
    infile.open(filepath);//infile >> str; //如果字符串有空格会被截断
    infile.getline(str,sizeof(str));//读取包含空格等特殊字符
    infile.close();
    cout << str << endl;}

在这里插入图片描述

#include<iostream>#include<fstream>#include<cstdlib>usingnamespace std;voidmain(){
    std::cout <<"Hello World!\n";constchar* filepath ="D:\\test.txt";{
        ofstream outfile;//创建输出文件流
        outfile.open(filepath,ios::app);//指定追加模式
        outfile <<"\r\n 这是追加的一行字符串文本,测试。";
        outfile.close();//切记,关闭生效system(filepath);}}

在这里插入图片描述
二进文件制读写:

#include<iostream>#include<fstream>#include<cstdlib>#include<string>#include<sstream>#include<limits>usingnamespace std;

std::string DoubleToString(constdouble value,unsignedint precisionAfterPoint){
    ostringstream sout;// 清除默认精度
    sout.precision(std::numeric_limits<double>::digits10);
    sout << value;

    std::string res = std::move(sout.str());auto pos = res.find('.');if(pos == std::string::npos)return res;auto splitLen = pos +1+ precisionAfterPoint;if(res.size()<= splitLen)return res;return res.substr(0, splitLen);}structInfo{char name[10];char pwd[10];int id;double price;};voidmain(){constchar* file ="D:\\bit.bin";

    cout <<"写入二进制文件:"<< endl;
    Info infos[3]={{"AAAA","a1a2a3",1,100.32},{"BBBB","b1b2b3",2,120.32},{"CCCC","c1c2c3",3,140.32}};
    ofstream ofs(file,ios::binary);//指定二进制方式
    ofs.write((char*)infos,sizeof(infos));//写入
    ofs.close();

    cout <<"读取二进制文件:"<< endl;
    Info tmpInfo[3]={0};
    ifstream ifs(file, ios::binary);
    ifs.read((char*)tmpInfo,sizeof(tmpInfo));
    ifs.close();for(auto& i : tmpInfo){
        cout << i.name <<' '<< i.pwd <<' '<< i.id <<' '<< i.price << endl;}

    cout <<"写入到文本文件中:"<< endl;constchar* filetxt ="D:\\info.txt";
    ofstream fout(filetxt);for(auto& i : tmpInfo){
        string str;
        str.append(i.name).append(" ").append(i.pwd).append(" ").append(std::to_string(i.id)).append(" ").append(DoubleToString(i.price,2)).append("\n");
        fout << str;}
    fout.close();system(filetxt);//打开文本文件

    cin.get();}

在这里插入图片描述
在这里插入图片描述
一个文件要读写同时执行则使用 fstream 流类。
文件指针演示:

#include<iostream>#include<fstream>#include<cstdlib>usingnamespace std;voidmain(){constchar* file ="D:\\filePtr.txt";{
        ofstream ofs(file);if(!ofs){
            cout <<"文件操作失败!"<< endl;}
        ofs <<"123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        ofs.close();}{
        ifstream ifs(file);if(ifs.fail()){
            cout <<"文件操作失败!"<< endl;}
        ifs.seekg(9, ios::beg);//从文件开始,移动文件的可读指针,移动步长为9char c;while(ifs.get(c))//读取剩下的每一个字符{
            cout << c;}
        cout << endl;
        ifs.close();}{
        cout <<"在指定位置插入文本!"<< endl;//ofstream ofs(file,ios::app); //追加模式对于移动文件指针无效。//ofstream ofs(file,ios::in|ios::out);//覆盖指针后面数据
        ofstream ofs(file);if(ofs.fail()){
            cout <<"文件操作失败!"<< endl;}
        ofs.seekp(9, ios::beg);
        ofs <<"******";
        ofs.close();system(file);}

    cin.get();}

在这里插入图片描述

标签: c++

本文转载自: https://blog.csdn.net/weixin_41049188/article/details/120095942
版权归原作者 haixin-561 所有, 如有侵权,请联系我们删除。

“C++ :输入/输出流”的评论:

还没有评论